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

Need Help wiht File Input String Manipulation to File Output 1

Status
Not open for further replies.

faust13

Programmer
Aug 7, 2001
176
US
I would like to read in three characters at a time, and evaluate them for a condition. Depending on that condition, write them to a file. Please find my code below.

Thanks!

Code:

import java.io.*;

public class ReadFile {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1){
out.write(c);
System.out.println(c);
}

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

----------------------------------------
Is George Lucas Kidding...
 
import java.io.*;

public class ReadFile {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c, charCount = 0, statistic_new = 0;
String strBuffer = "", tempStr = "";
while ((c = in.read()) != -1){
if (c==10 || c==13) continue; // newline code or return code
if (c==9) continue; // tab code
if (c==32) continue; // space
/*
charCount ++;
strBuffer = strBuffer + (char)c;
if (charCount%3==0)
{
if ((strBuffer.toUpperCase()).equals("NEW")) statistic_new++;
charCount==0;
}
*/
// to gather 3 chars a time
if (strBuffer.length()==3) strBuffer = strBuffer.substring(1,3);
System.out.println(strBuffer);
strBuffer = strBuffer + (char)c;
if (strBuffer.length()!=3) continue;
else
out.write((char)c);
if ((strBuffer.toUpperCase()).equals("NEW")) statistic_new++;
// end of gather 3 chars a time
}
System.out.println("");
System.out.println("Number of words 'new': "+statistic_new);
in.close();
out.close();
}
}

you can use my modified program as input.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top