• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input from text file  RSS feed

 
Ray Bell
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this seems so basic. But could someone show me an example of how to use input in aprogram from a text file instead of user input or instead of me inputting a series of numbers for example.
Wher would I store these files. Same with output files. How do I store program output in a file and where do I find the file. Im using netbeans.

Neither my prof nor my textbook really explain this..
Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Oracle tutorial on I/O is a good place to start...

https://docs.oracle.com/javase/tutorial/essential/io/index.html

Henry
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Ray Bell
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I did read some of the Oracle tutor ial. This is too advanced for me right now.
Lets say Im writing a short program and I want to input 4 numbers from a text file. I can create a file in notepad but where would I save it in order to access it from netbeans?
Also originally I used Scanner input to input numbers from a user. How do I change that to input numbers from the file instead.
Then instead of outputting the output of System.out.print... I want the output to go to an output file instead.
It would be really helpful if someone showed me a small sample code that does that
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Bell wrote:I did read some of the Oracle tutor ial. This is too advanced for me right now.


Not sure if I understand your point here. So, code examples with explanations are too advanced, but examples without explanation are fine? There are examples in the tutorial that does everything you requested.

Ray Bell wrote:
Also originally I used Scanner input to input numbers from a user. How do I change that to input numbers from the file instead.


No change to the reading code needed. The difference between scanning from standard input, and a file, is merely how it is initialized -- as explained in the tutorial.

Henry
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Bell wrote:. . . This is too advanced for me right now. . . .
No, it isn't.
The easiest way to read numbers from a text file is probably with a Scanner. For the code following you must ensure that what is in the text matches what the Scanner expects, or vice versa. I have written a throws clause (which I don't usually do) so as to move the Exception handling out of the method shown below. I would usually use try with resources, but that would make the code look complicated, which it isn't.
You can provide a Path instance to the Scanner constructor and then use the Scanner to read individual numbers. Easy‑peasy.
  • It is probably simplest to provide an absolute path (small p) to your file:-
     String pathName = "C:/Users/Ray/My Documents/myNumbers.txt";
      Use / not \. An absolute path will be the same wherever you run your program from.
  • Use the Paths class to create the Path:-
     Path path = Paths.get(pathName);
  • Use that Path to create a Scanner object:-
     Scanner fileScan = new Scanner(path);
  • Read the int and close the Scanner:-
     int i = fileScan.nextInt();
     fileScan.close();

  • And Bob's Your Uncle. You can run the first three stages together into one line as in the example following:-The numbers.txt file:-You have to provide the correct pathName as an argument to that method.
    Because I am using args, I have to provide the name of the file as a command line argument:-
    At the command line, I wrote:$ javac FileReadDemo.java
    $ java FileReadDemo numbers.txt
    7437
    Don't write the dollar signs.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!