Results 1 to 6 of 6

Thread: Problem in reading file in java

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Problem in reading file in java

    Hello,
    I work on a code that reads a csv file (and performs calculations based on information) I tested the program on a file "test", and it works. The problem is that the file "prod" is not read.
    Code:
    Exception occurred during event dispatching:
    java.lang.OutOfMemoryError
    <<no stack trace Available>>
    Note that the file "test" contain 15 fields, while the file "prod" contains 600. It is more than 15MB. Nothing happens in the panel config:-Xmx300M Java. Basically I have a problem in reading a file. I have tried all my possibilities but, none worked for me.

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Problem in reading file in java

    Hello,
    Even I have a similar kind of a problem with my code. I think it's a character story line end, because my function that retrieves the line to bring in an element vector stops on a newline or `\ n '. But when I copy 1 single line (wordpad) File "test" and that I test program. With a software that shows all the characters I see nothing.

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Problem in reading file in java

    Hello,
    This is my code, please check it and if there are any mistakes then please do guide me.
    Code:
    Public Vector fgets(String file){
            
            String rtch = "\ n";
            Vector v = new Vector();
            String str = stringReadFromFile(file);
            StringTokenizer line = new StringTokenizer(str, rtch);
            while (line.hasMoreTokens())
    	    v.addElement(line.nextToken());
            return v;
            }
        }

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Problem in reading file in java

    Hey guys
    This is my part of code, just have a look at it.
    Code:
    Public String stringReadFromFile(String r_file){
            File f;
    FileReader in = null;
    String bk = "";
    	try{
    	    f = new File(r_file);
    in = new FileReader(f);
    	    int sz = (int) f.length();
    	    char[] dt = new char[sz];
    	    int chrd = 0;
    	    while(chrd <sz)
    	        chrd + = in.read(dt, chrd, sz - chrd);
    return = new String(dt);
                in.close();
                return bk.substring(0, return.length());
    	    }
    	catch(Exception e){
    	    return "2";
    	    }
    	}

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Problem in reading file in java

    Hello,
    If it is to read line by line, both directly use a BufferedReader. In passing it must close the file in a finallyAnd prefer ArrayList to Vector in a single-threaded. Just see the code below
    Code:
    Public List fgets <String>(String file) throws IOException{
            ArrayList lt = <String> new ArrayList <String>();
            BufferedReader br = new BufferedReader(new FileReader(file));
            try {
            	String ln;
            	while ( (ln = br.readLine()) != null) {
            		lt.add(ln);
            	}
            } finally {
            	br.close();
            }
            lt.TrimToSize();
            return lt;
        }

  6. #6
    Join Date
    Dec 2009
    Posts
    192

    Re: Problem in reading file in java

    Hello,
    Error IOExpection , I recovered on another class upstream. I copied your function but it leaves me an error that I can not understand.
    Code:
    "WriteReadFile.java": <identifier> waited in line 151, column 17
    What does this means. Do you have any idea about it. Thank you in advance.

Similar Threads

  1. Reading an XML file in Java
    By Chetna in forum Software Development
    Replies: 7
    Last Post: 13-07-2011, 04:31 PM
  2. Problem in copying a file in java
    By Messenger in forum Software Development
    Replies: 6
    Last Post: 04-08-2010, 11:09 AM
  3. Problem reading a text file
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 04:02 AM
  4. Java reading from a file
    By xqc72 in forum Software Development
    Replies: 1
    Last Post: 20-11-2009, 08:20 AM
  5. Error: problem opening file for reading
    By Digamber in forum Operating Systems
    Replies: 3
    Last Post: 26-08-2009, 07:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,297,445.14854 seconds with 17 queries