This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/9/25

read and write to a java file gives runtime error

divinity divinity

2017/9/25

#
hi danpost I am also doing another program where I have to read and display a file in java. I tried many codes but keeping getting a runtime error. here I am trying another set of codes here is the codes.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor;
 */
package newfile;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;



/**
 *
 * @author luanataylor
 */
public class NewFile {

    /**        

     * @param args the command line arguments
     */
    public static void main(String[] args) throws  IOException{
        
       
       StringBuilder sb = new StringBuilder();
       File f = new File("file.txt");
       
       BufferedReader br = null;
       
       try{
           
           br = new BufferedReader(new FileReader("file.txt"));
           
           String line;
           while((line = br.readLine()) != null){
                if(sb.length() > 0){
                    sb.append("\n");
                }
                sb.append(line);
           }
          
       }
       catch(IOException e){
           e.printStackTrace();
       }
       finally {
           
           try{
               
               if(br != null){
                   br.close();
               }
           }
           catch(IOException ex){
               ex.printStackTrace();
           }
       }
       String contents = sb.toString();
        System.out.println(contents);
        
 }
}

and here is the printout, the error
java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:146)
	at java.io.FileInputStream.<init>(FileInputStream.java:101)
	at java.io.FileReader.<init>(FileReader.java:58)
	at newfile.NewFile.main(NewFile.java:36)
BUILD SUCCESSFUL (total time: 0 seconds)
can someone tell me what was the cause and how I can correct. this any help would be appreciated
danpost danpost

2017/9/25

#
Remove line 30 (since you do not use the File object named 'f' anywhere). I am not absolutely sure that this change will have any bearing on fixing your issue. Report back as to results.
divinity divinity

2017/9/25

#
hi danpost I have remove line 30 and I am still getting the same result. here it is same runtime error
java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:146)
	at java.io.FileInputStream.<init>(FileInputStream.java:101)
	at java.io.FileReader.<init>(FileReader.java:58)
	at newfile.NewFile.main(NewFile.java:36)
BUILD SUCCESSFUL (total time: 1 second)
danpost danpost

2017/9/25

#
Make sure that you have the file named correctly (that the file name on line 36 matches exactly with the file name in the directory. Also, make sure that the file is located in the same directory as where the 'NewFile.java' file is located.
divinity divinity

2017/9/25

#
I got it. apparently the file wasnt there. thanks danpost
You need to login to post a reply.