File I/O Please help!
Hi i need help with a simple I/O problem. I have a large text file, which i want to break up into smaller files. I want to break the file each time "From -" is found at the start of a new line.
public static void main(String[] args) throws Exception {
String path1 = "file1";
BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(path1)));
BufferedWriter out = new BufferedWriter(new FileWriter("Emails//out.txt"));
String line;
while((line = br.readLine()) != null)
{
if(line.startsWith("From -")){
//send from here to the next "from -" into a text file
}
}
}
}
Above is as far as ive come, i dont know how to send each block of text to an individual file, can anybody help, it would be greatly appreciated!
Thanks
[988 byte] By [
cupanTae] at [2007-11-11 7:51:02]

# 1 Re: File I/O Please help!
I'd recommend using the Scanner class, and setting the delimiter to "From -"
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("file.txt")).useDelimiter("From -");
String line;
for (int i = 0; scanner.hasNext(); i++) {
PrintWriter pw = new PrintWriter(new FileWriter("file" + i + ".txt"));
pw.println(scanner.next());
pw.close();
}
} catch (IOException e) { e.printStackTrace(); }
}
Try something like that. I haven't tested it yet, so there's a good chance it won't compile, but yeah, hopefully you get the idea.
[edit] You're going to need to import java.util.Scanner for the Scanner class.
destin at 2007-11-11 22:37:09 >

# 2 Re: File I/O Please help!
Thanks, this code compiles fine, but it doesn't write to the file specified. Im not familiar with the scanner class. What is the
PrintWriter pw = new PrintWriter(new FileWriter("file" + i + ".txt"));
i in the above piece of code?
# 3 Re: File I/O Please help!
Thanks, this code compiles fine, but it doesn't write to the file specified. Im not familiar with the scanner class. What is the
PrintWriter pw = new PrintWriter(new FileWriter("file" + i + ".txt"));
i in the above piece of code?
Well, in the OP, you said that you wanted to write each one out to different
files, so that's what I made it do. It'll write the first post to file0.txt, the second
to file1.txt, the third to file2.txt, etc. i is defined in the for loop.
destin at 2007-11-11 22:39:13 >

# 4 Re: File I/O Please help!
I have written some code that will split a file using a deliminator in the example below the deliminator is "-". Probably not the most clean code but it works. Let me know if it works out well for you.
import java.io.*;
public class SplitText
{
FileOutputStream Save;
PrintStream printString;
String strWholeMessage="";
int i=0;
public static void main(String args[])
{
String myFile="File.txt"; //Change the file to read here.
SplitText newSplitText = new SplitText();
newSplitText.ReadSplit(myFile);
}
public void ReadSplit(String myFile)
{
try
{
File my_File = new File(myFile);
BufferedReader in = new BufferedReader(new FileReader(my_File));
String strString;
while ((strString = in.readLine()) != null)
{
strWholeMessage = strWholeMessage + strString;
}
in.close();
String[] splitText = strWholeMessage.split("-"); //Change the deliminator here.
while(i < splitText.length)
{
try
{
Save = new FileOutputStream("OutPut" + i + ".txt"); //Change the output file here. File starts from OutPut0.txt
printString = new PrintStream(Save);
printString.println (splitText[i]);
printString.close();
}
catch (Exception e)
{
System.err.println ("Unable to write to file");
}
i++;
}
}
catch (IOException e)
{
System.out.println("Unable to open file");
}
}
}
You can find some java examples from the following site www.java.codeyourself.com. Not many at the moment. Would really appreciate people sending in code. :WAVE:
major at 2007-11-11 22:40:12 >

# 5 Re: File I/O Please help!
Yes, got that working thanks very much for the help!