Results 1 to 15 of 15
  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Problem with file I/O

    Hi.
    Im making a simple file reader, need it for another programm, mostly just experimenting, have no expiriance with that in Java and ive encountered something odd, maybe my code or something. At start, my second file was missing 3 lines, added a larger buffer - problem solved. After running this, the input/output files are different sizes, the new one is missing 4kb, why? And another thing, what woulb be the best method of reading large files(1,7MB .txt)?
    Thanks!

    Java Code:
    package Files;
    import java.io.*;
    
    public class File {
    
    	int[] array=new int[10];
    	
    	public static void main(String[] args) {
    		
    try
    {
    	FileInputStream fstream=new FileInputStream("text1.txt");
    	FileOutputStream OutStream=new FileOutputStream("text2.txt");
    	
    	DataInputStream in=new DataInputStream(fstream);
    	DataOutputStream out=new DataOutputStream(OutStream);
    	
    	BufferedReader br=new BufferedReader(new InputStreamReader(in), 2048);
    	BufferedWriter br2=new BufferedWriter(new OutputStreamWriter(out), 2048);
    	
    	
    	
    	String strLine=null;
    	while((strLine=br.readLine())!=null)
    	{
    		//System.out.println(strLine);
    		br2.write(strLine);
    		br2.newLine();
    	}
    	in.close();
    	out.close();
    }
    catch (Exception e)
    {
    	System.err.println("Error: "+ e.getMessage());
    }
    	}
    
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Problem with file I/O

    If you are simply moving the data and not doing anything with it in between reading and writing then just use the Input and Output streams and a byte[] buffer.
    No need for all those other in between streams, which may or may not be the cause of your problem.
    Java Code:
    .. open the streams
    byte[] buffer = new byte[8192]; // 8k buffer
    int byteCount;  // so we know how much was read in each loop
    while((byteCount = fstream.read(buffer)) > -1) {
       outstream.write(buffer, 0, byteCount);
    }
    .. close it all off (in a finally block).
    That will read anything from a to b, and take only a handful of k to do it (assuming b is not in memory).
    You seemed to be overcomplicating things.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Problem with file I/O

    I have to read a large file, line by line, then divide by separator(convert to longint) and put it in a data structure, print out any line the user wishes. The data structure is clear, im just curios about how to make the reading part. Thanks.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Problem with file I/O

    OK.
    Take out all those streams and do:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader("your file name"));
    similarly for the writer (note this really ought to be done such that you can close the readers in a finally block).

    I say this because I have just noticed you are closing the (utterly unecessary) DataOutputStream.
    You are not closing the BufferedWriter.
    The BufferedWriter which (as its name suggests) buffers the output.
    So you close the underlying stream, which has no effect on anything which may still be held in that buffer.

    Doing the above (just declaring the BufferedReader/Writer) will prevent this from happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with file I/O

    Also, at the end close br and br2 instead of in and out; there may still be data in those bufferers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Problem with file I/O

    Thanks guys, did all the above and everything is how it should be. Got to do some reading on file handling or wait till the lectures touch the subject.
    One last question regarding dividing the string up. What should i use? As far as googling told me, that the best option should be using split(), is that right? Or maybe use chartoarray to make things complicated?

  7. #7
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with file I/O

    Quote Originally Posted by MustSeeMelons View Post
    Thanks guys, did all the above and everything is how it should be. Got to do some reading on file handling or wait till the lectures touch the subject.
    One last question regarding dividing the string up. What should i use? As far as googling told me, that the best option should be using split(), is that right? Or maybe use chartoarray to make things complicated?
    split is the best, for example you have in your text "lol=5" you can create an array (We'll call it varHold) and you can do "varHold = string.split("=")" and that way
    varHold will have two strings varHold[0] would me "lol" and varHold[1] would me "5"
    WARNING I am Russian so it's possible that I wont understand you correctly...

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Problem with file I/O

    Quote Originally Posted by JosAH View Post
    Also, at the end close br and br2 instead of in and out; there may still be data in those bufferers.

    kind regards,

    Jos
    Oi...that's what I said!
    :p
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Problem with file I/O

    Thanks, split was really simple, program is running almost perfectly, but i have a question regarding Exceptions.
    How do i create custom mesages? I need 3 in total, file not found, delimiter not found and the the user asks to print a line that does not exist.
    File not found works by default, but the other two are messing eash other around. Should i create my own exceptions? Cant even asks the question properlly..

  10. #10
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with file I/O

    Try and catch
    Java Code:
    try{
    Scanner lol = new Scanner(new File("file.ini"));
    catch (Exception e){ 
    System.out.println("File not found");
    }
    Easy enough :D
    WARNING I am Russian so it's possible that I wont understand you correctly...

  11. #11
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Problem with file I/O

    Lol indeed, i know that much :D Here is the problem. If no file is found, there is an exception, if the delim is wrong, there has to be an exception, and if out of bounds occurs, there has to be an exeption. If the delim is cought, it outputs it all the time and just stops.
    Java Code:
    public void magic()
    	{
    		ListOfRows RowList=new ListOfRows();
    		try
    		{
    			BufferedReader buffer=new BufferedReader(new FileReader(location), 2048);
    			String Line;
    			
    			while((Line=buffer.readLine())!=null)
    			{
    //				if(Line.matches(delim)==false)
    //				{
    //					buffer.close();
    //					throw new Exception("Delim fault");
    //				}
    				ListOfNumbers NumberList=new ListOfNumbers();
    				String[] Temp=Line.split(delim);
    				for(int i=0;i<Temp.length;i++)
    				{
    					Long StringNumber=Long.parseLong(Temp[i]);
    					Number NewNumber=new Number(StringNumber);
    					NumberList.add(NewNumber);
    				}
    				RowList.add(NumberList);
    			}
    			buffer.close();
    			System.out.println("Row count: "+RowList.size);
    			System.out.println("Number count in row: "+RowList.getNode(rowPrint).Cur.size);
    			RowList.getNode(rowPrint).Cur.print(order);
    			System.out.println();
    		}
    		catch(Exception e )
    		{
    			System.err.println("Error: "+e.getMessage());
    		}

  12. #12
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with file I/O

    Well of course it will close, because if anything inside of the Try fails, it stops that to try. You should do multiple tries
    WARNING I am Russian so it's possible that I wont understand you correctly...

  13. #13
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with file I/O

    Like:
    Try
    find file
    fileFound = true
    catch "could not find file"
    if fileFound then
    do something with the file
    WARNING I am Russian so it's possible that I wont understand you correctly...

  14. #14
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Problem with file I/O

    Yeah, understood that, thanks. If there is a possibility to throw somwthing, i just would have to make a seperate try? A try, for finding the dile, a try, to check the delimiter and a try for out of bounds? And another question, if i have two exceptions throwable in one try, then i need only one catch? Because if i had two, both would catch them?

  15. #15
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with file I/O

    The one that fails will return an exception
    But make sure you add the Try only where it really has to be
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 04:01 AM
  2. Problem with file BufferedReader using binary file.
    By allymoha in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2011, 10:01 AM
  3. Serious Jar-file problem
    By Igbear in forum Advanced Java
    Replies: 12
    Last Post: 04-27-2011, 11:08 PM
  4. Got problem with jar-file, any help?
    By Igbear in forum Advanced Java
    Replies: 7
    Last Post: 11-20-2010, 10:10 PM
  5. File problem
    By Blackberrylerner in forum New To Java
    Replies: 29
    Last Post: 08-20-2010, 03:00 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
  •