input from file in java -


i java beginner learner , trying output data on file call a.txt. need have no idea y getting exception error file not open . put a.txt in same directory in have main , readfile.

- main path : c:\users\navdeep\desktop\java\assign1\src\assign1  - readfile : c:\users\navdeep\desktop\java\assign1\src\assign1  - a.txt :  c:\users\navdeep\desktop\java\assign1\src\assign1 

thanks in advance .

main.java

package assign1;  public class main {     public static void main(string[] args) {         readfile r = new readfile();         r.openfile();         r.readfile();         r.closefile();     } } 

readile.java

package assign1;  import java.io.*; import java.util.*;  public class readfile {     private scanner x;      public void openfile() {         try {             x = new scanner(new file("a.txt"));         } catch (exception e) {             system.out.println("file not open \n");         }     }      public void readfile() {         while (x.hasnext()) {             string agent = x.next();             string request_type = x.next();             string classtype = x.next();             string numberofseat = x.next();             string arrivaltime = x.next();              system.out.printf("%s %s %s %s %s \n", agent,                      request_type, classtype, numberofseat, arrivaltime);         }     }     public void closefile() {         x.close();     } } 

a.txt

1 r e 1 0 2 r e 1 1  

if use file relative path, assumed relative "current user directory". what's "current user directory"? see doc:

a relative pathname, in contrast, must interpreted in terms of information taken other pathname. default classes in java.io package resolve relative pathnames against current user directory. directory named system property user.dir, , typically directory in java virtual machine invoked.

also doc:

on unix systems, relative pathname made absolute resolving against current user directory. on microsoft windows systems, relative pathname made absolute resolving against current directory of drive named pathname, if any; if not, resolved against current user directory.

so 1 way file found using relative path start jvm in directory file.

however, approach can kind of limiting since constrains start jvm in directory.

as alternative, might consider using classloader#getresourceasstream. allows load resource on jvm's "classpath". classpath can configured in number of different ways, including @ launch time using arguments jvm. suggest using that, rather initializing scanner file. like:

inputstream = stackoverflow.class.getclassloader().getresourceasstream("a.txt"); scanner scanner = new scanner(is); 

now, when using getresourceasstream, have make sure file referenced on classpath of java virtual machine process holds program.

you've said in comments you're using eclipse.

in eclipse, can set classpath execution doing following:

1) after running program @ least once, click on little dropdown arrow next bug or play sign.

2) click on "debug configurations" or "run configurations".

3) in left sidebar, select run configuration named after program you're running

4) click on "classpath" tab

5) click on "user entries"

6) click on "advanced"

7) select "add folders"

8) select folder a.txt resides.

once have done this, can run program using run configuration have set up, , a.txt found.

basic idea of classpath

the classpath represents resources jvm (java virtual machine) holding program knows while it's running. if familiar working command line, can think of analogous os's "path" environment variable.

you can read in depth here.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -