How to print an input file?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestBookCollection {
public static void main(String args[]){
File books = new File ("C:\\books.txt");
System.out.println(books);
}
}
Output: C:\books.txt
I know that it is input correctly but how do I get the content within the actual file to print?
Re: How to print an input file?
Quote:
how do I get the content within the actual file
You need to read the lines from the file so you can print them.
The Scanner class is easy to use for text files. Define an instance of Scanner with the File and use its method to read the lines from the file.
Re: How to print an input file?
Re: How to print an input file?
----------------------------------------------------------------------------
Initially, read the file and contents'
----------------------------------------------------------------------------
'
Code:
FileReader fx=new FileReader("D:/output.txt");
BufferedReader bx=new BufferedReader(fx);
'
----------------------------------------------------------------------------
Declare a String and put contents in String
----------------------------------------------------------------------------
'
Code:
String holdcontents;
holdcontents=bx.readLine();
'
----------------------------------------------------------------------------
Display the String'
----------------------------------------------------------------------------
'
Code:
System.out.print(holdcontents);
'