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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileInputStream + FileOutputStream

Status
Not open for further replies.

yytan

Programmer
May 8, 2002
113
MY
hi there;
<p>
i am reading a file and then write it to another new file. i have some problem when i want to check again some word.

<pre>
/* Copy a text file.

To use this program, specify the name
of the source file and the destination file.
For example, to copy a file called FIRST.TXT
to a file called SECOND.TXT, use the following
command line.
java CopyFile FIRST.TXT SECOND.TXT
*/

import java.io.*;

class CopyFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;

try {
// open input file
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println(&quot;Input File Not Found&quot;);
return;
}

// open output file
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println(&quot;Error Opening Output File&quot;);
return;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println(&quot;Usage: CopyFile From To&quot;);
return;
}

// Copy File
try {
do {
i = fin.read();
System.out.println(&quot;i = &quot; + i);
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println(&quot;File Error&quot;);
}

fin.close();
fout.close();
}
}

</pre>

when i System.out.println(&quot;i = &quot; + i); it return a integer for me, how if i want to check for word? for example,
<p>
if i = &quot;tan&quot; then call another function.....
 
If you files are text files, use a BufferedReader then use readLine() method rather than read(). A String will be returned (line), a StringTokenizer can then be used to split the line up into words (separated by a white space, e.g. tab, space).

So, you will need to create a BufferedReader, BufferedWriter.

BufferedReader in = new BufferedReader(new FileReader(&quot;foo.in&quot;));

and a StringTokenizer;

StringTokenizer st = new StringTokenizer(line,&quot; \t&quot;);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
 
thekobbler;

i am glad to receive your reply, i have tried using ur method. the problem is, the original source &quot;txt report format file&quot; let say having 5 lines of text (without a new line in between each line).

when i copy to a new file, it becomes 10 lines, meaning, after it read for the first line, it return a newline().

i view using notepad, i can't see any escape character at the end of each line, but when i edit using dos, i can see every end of each line having a special character, i think that might cause [using buffered reader] enter a new line after copy 1 line from original source file.

when i using FileInputStream method, it copy exactly as the original source file. the problem is, i dont know how to check the word when i read(), i also want to getLineNumber() coz this copy file program need to modify and add something during the process.
 
Why not append the char's as they are read to a StringBuffer, until a &quot;whitespace&quot; is detected (i.e. space, tab, End-of-line), then get the word using stringBuffer.toString(); and empty the stringBuffer.

Note, in UNIX an EOL is \r\n, whereas in MSWindows/DOS it's \n. So you may need to check that there are char's in the stringBuffer before outputting the word.
 
dear the kobbler;

thanks for ur suggestion.

anyway, i am using buffered reader to find the position of the char i want to find, then i use FileInputStream to copy byte by byte to a new text file.

before, i was asking for the System.out.println(&quot;i = &quot; + i); it returns integer, then i realized it was in ascii format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top