File I/O Please help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: File I/O Please help!

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    Arrow 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

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    16
    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?

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote 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.

  5. #5
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    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.

  6. #6
    Join Date
    Oct 2005
    Posts
    16
    Yes, got that working thanks very much for the help!

Similar Threads

  1. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 03:22 AM
  2. Either Overwrite or Rename file
    By Shaitan00 in forum Java
    Replies: 1
    Last Post: 04-06-2005, 06:04 AM
  3. Tomcat FILE I/O
    By Souren in forum Java
    Replies: 0
    Last Post: 04-29-2003, 01:10 AM
  4. File I/O...
    By Brian Patrick in forum VB Classic
    Replies: 0
    Last Post: 02-25-2001, 02:12 PM
  5. File i/o
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center