Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Write to multiple files  RSS feed

 
Mark Mitchell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a program that reads a text file, line by line. The values are not in any particular order.
Here is an example of the data in the file:

GIUO 1KB
YUTE 2KB
POIFE 3KB
TTEE 0Items
OIOIU 1KB
EWWW 3KB

I want to create a file that puts values with 1KB in there own file, values with 3KB in there own file, etc.
Here is my question, can I read from the main file and insert these values in their new files simultaneously? (As I read from the main file can I insert the values for GIUO 1KB and OIOIU 1KB into the 1KB file I created? Can I insert the values for EWWW 3KB and POIFE 3KB into the 3KB file I created?) Does this make sense?

This is what I have so far:

String inFileName = request.getParameter("mainFile");

try
{

File inputFile = new File(inFileName);
File outputFile = new File(outFileName);
if(inputFile == null){ out.println("<br>input file is null");}
if(outputFile == null){ out.println("<br>outputFile is null");}

FileReader inFile = new FileReader(inputFile);
FileWriter outFile = new FileWriter(outputFile);

BufferedReader br = new BufferedReader(inFile);


String lineOfText = " ";
int count = 0;

while((lineOfText = br.readLine())!=null)
{
if(lineOfText.toUpperCase().indexOf(textSearch.toUpperCase())!= -1) //value to search
{

count++;
out.println("<br>"+count + ". " + lineOfText); //screen print
outFile.write(lineOfText + "\r\n"); //file print
lineOfText = "";
}

} //end while


br.close();
outFile.close();
inFile.close();

}
catch(Exception e)
{
out.println(e);
}

thanks,
Mark
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, simply open multiple output files (you'll want to change the variable name from "outFile" to something different for each File, of course).

A couple more pointers: the "new" operator either returns a non-null reference or throws an exception -- it never returns null. Thus, you don't need the comparisons in your code. I don't think that File's constructor throws any exception, though (check the JavaDocs to be sure), since it doesn't verify that the file exists. It's just a pointer to a "possible file on disk."

When posting code, surround it with [ CODE ] ... [ /CODE ] tags (no spaces inside []s) or use the UBB Code button below the edit box. This will keep your indentation which makes it easier to read your code. Of course, you can edit your own posts (use pencil and paper icon above post) to add them as you see fit.
 
Mark Mitchell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!