-
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
-
I'd recommend using the Scanner class, and setting the delimiter to "From -"
Code:
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.
Last edited by destin; 01-26-2006 at 04:05 PM.
-
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?
-
 Originally Posted by cupanTae
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.
-
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.
Code:
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.
-
Yes, got that working thanks very much for the help!
Similar Threads
-
By Arjuna in forum Database
Replies: 2
Last Post: 07-25-2007, 03:22 AM
-
By Shaitan00 in forum Java
Replies: 1
Last Post: 04-06-2005, 06:04 AM
-
Replies: 0
Last Post: 04-29-2003, 01:10 AM
-
By Brian Patrick in forum VB Classic
Replies: 0
Last Post: 02-25-2001, 02:12 PM
-
By wannabe in forum VB Classic
Replies: 0
Last Post: 02-15-2001, 03:35 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks