Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from a text file and storing it in another file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need to extract certain information from a text file and then store this information in another file.
What would be the best way to do this - some sample code would be much appreciated!
 
String file1 = "file1.txt";
String file2 = "file2.txt";

FileInputStream in = new FileInputStream(file1);
FileOuputStream out = new FileOutputStream(file2);

int c;
while ((c = in.read()) != -1)
out.write(c);

in.close();
out.close();

Note that the above does not include any exception handling or any buffering but simply reads input and writes it out to a new file.

I strongly suggest that you browse the java I/O API.

Charles
 
Can you please tell me why the following code will not compile??

I get compiler errors saying "illegal escape character" pointing at - File file = new File("C:\Windows\Documents and Settings\citemp1\My Documents\My Documents\test.doc");

and with

File file2 = new File("C:\JAVA\Test\test1.txt");

see code below!!
============================================================


import java.io.*;
import java.util.*;

public class Test
{
public static void main(String[] args)
{
try
{
LinkedList myList = new LinkedList( );
File file = new File("C:\Windows\Documents and Settings\citemp1\My Documents\My Documents\test.doc");

Bufferedreader reader = new BufferedReader( new FileReader(File) );

String str = "";
while( (str = reader.readLine( )) != null )
{
myList.add( str );
}
File file2 = new File("C:\JAVA\Test\test1.txt");
PrintWriter writer = new PrintWriter( new BufferedWriter( new FileWriter( file2 ) ) );

Iterator iter = myList.iterator();
while(iter.hasNext())
{ String str1 = (String) iter.next();
if(str1.equals("1") )
{ writer.print(str1);
}
}
}
catch( Exception e )
{
System.out.println("ERROR");
}

}
}

============================================================

Thanx
 
In Java, you have to use unix style path names or use double slashes:

"C:/Java/Test/test1.txt"

or

"C:\\Java\\Test\\test1.txt"
 
Thank you.

Ok...my code now compiles and runs but it catches an error instead of doing what I want it to do.

I'm just doing a test but it catches the exception. Do you think my try block is doing what I want it to do. I have attached a 'buffer reader' to a 'file reader' so that the whole file is read into a buffer.

So when my file is read into the buffer I want to go through the buffer and when I come to some particular text I want to output something in another file.

see code below
============================================================

import java.io.*;
import java.util.*;

public class Test
{
public static void main(String[] args)
{
try
{

LinkedList myList = new LinkedList( );
File file = new File("C:\\Windows\\Documents and Settings\\citemp1\\My Documents\\My Documents\\test.doc");

BufferedReader reader = new BufferedReader( new FileReader(file) );

/*
Here's the nice part. When the reader gets to the end of a file
i.e. the EOF char. It returns null :eek:)
*/
String str = "";
while( (str = reader.readLine( )) != null )
{
myList.add( str );
}
File file2 = new File("C:\\JAVA\\Test\\test1.txt");
PrintWriter writer = new PrintWriter( new BufferedWriter( new FileWriter( file2 ) ) );

Iterator iter = myList.iterator();
while(iter.hasNext())
{ String str1 = (String) iter.next();
if(str1.equals("xyz") )
{ writer.print("Test successful");
}
}
}
catch( Exception e )
{
System.out.println("ERROR");
}

}
}

============================================================
Thanks in advance
 
Try doing:

catch (Exception e) {
e.printStackTrace();
}

so we can find out what the problem is.
 
Ok - here is what it says after runing it!!

============================================================

java.io.FileNotFoundException: C:\Windows\Documents and Settings\citemp1\My Docu
ments\My Documents\test.doc (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileReader.<init>(FileReader.java:51)
at Test.main(Test.java:17)
============================================================
 
Ok...my fault..I got the path wrong (stupid mistake!!)

Ok...now it runs and creates the file (test1.txt) but it
doesn't wite to it when I want it to!

I beleive the problem area with my code is as below

============================================================

PrintWriter writer = new PrintWriter( new BufferedWriter( new FileWriter( file2 ) ) );

Iterator iter = myList.iterator();
while(iter.hasNext())
{ String str1 = (String) iter.next();
if(str1.equals(&quot;9&quot;) )
{ writer.print(&quot;Test successful&quot;);
}
}

============================================================

Thanx again
 
hi
i am kind of new to java and i would like to know how i can read a text file with each line containing numbers and words and then get the program to display all the word on each line in reverse order

some sample codes would help a lot as i have been trying to figure this out for the last 2 weeks

thankyou
 
Wrap a BufferedReader around a FileReader. This will give you the ability to read line by line. Next you will want to use a StringTokenizer to break up each line into separate words. Displaying these in reverse order should then be a piece of cake to figure out.

Consult Javadocs for more information on how to use the BufferedReader, FileReader, and StringTokenizer classes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top