Results 1 to 3 of 3
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default Files, cannot read!

    Alright, so I'm trying to get this file-stuff working. i want to create a .txt document and read through it, but my program doesn't seem to find it...:confused: actually, most of this are copied from my teachers example-program, I wanted to try it out, but I can't figure out what I'm doing wrong...

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JOptionPane;
    
    public class produktinfo {
    
    	private String id, price, name;
    	private String produkt="produkt.txt";
    	
    	public void read(String nameIn)throws IOException{
    		FileReader readfile=new FileReader(produkt);
    		BufferedReader inmatning=new BufferedReader(readfile);
    			while ((id=inmatning.readLine())!=null);
    			name=inmatning.readLine();
    			price=inmatning.readLine();
    			if(name.equals(nameIn)){
    				JOptionPane.showMessageDialog(null, price+name+id);
    			}		
    			inmatning.close();
    	}
    		
    }
    Java Code:
    import java.io.IOException;
    import javax.swing.JOptionPane;
    
    
    public class hanterare {
    public static void main (String[] args) throws IOException{
    	String name;
    	produktinfo bla = new produktinfo();
    	name=JOptionPane.showInputDialog("produkt namn");
    	bla.read(name);
    	}	
    }
    i've created a .txt document named produkt.txt and put it in every map(just to be sure) the bin map, the src map & project map and yet, it says:

    Java Code:
    Exception in thread "main" java.io.FileNotFoundException: produkt.txt (Det går inte att hitta filen) [COLOR="Blue"]// "file cannot be found" in the paranthesis[/COLOR]
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.io.FileReader.<init>(Unknown Source)
    	at produktinfo.read(produktinfo.java:14)
    	at hanterare.main(hanterare.java:11)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Your program has a 'current working directory'. You can print the value of it like this:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    You are supplying just the name of a file without any path so your program expects it to be in the current working directory. It isn't there. There are two ways to cure this little problem:

    1) stick the file in the current working directory.
    2) supply the entire path to the file.

    kind regards,

    Jos

  3. #3
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    you have to insert file in absolute path instanced of relative like this:
    D:/foo/bar/file.txt

Similar Threads

  1. Read binary files
    By ronaldo121 in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 04:37 AM
  2. Read/Write Files
    By FlyNn in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 09:45 PM
  3. Read and Open *.txt Files? help!!
    By ashton in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:42 PM
  4. How to use FTP to read files from server?
    By user12345 in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 01:46 PM
  5. How Read and Write XMl files using Java
    By tjs in forum SWT / JFace
    Replies: 0
    Last Post: 02-23-2009, 01:19 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
  •