Quote:
Originally Posted by sandyw
Hello everyone.
I need some help here.
I can not figure out where to put my .txt info at.
I 'm trying to use my instructure, some what confused. I have also read the info form Java and they really don't give an example on how to use a scanner where the user is prompt to find a text file once the file is found it then will display it
here is my code so far
-
public class Reading{
-
public static void main(String[] args) throws Exception {
-
Scanner kbd = new Scanner(System.in);
-
int choice;
-
System.out.println("Make a Section: ");
-
System.out.println("1. Read Text File ");
-
System.out.println("2. Copy Text File ");
-
System.out.println("3. Exit ");
-
System.out.print("\nPlease press Enter afer each response");
-
System.out.println("Enter your choose please: ");
-
choice = kbd.nextInt();
-
kbd.nextLine();
-
if (choice == 1) { // if 1 is select go to makePerson
-
String fname;
-
-
FileInputStream fin;
-
-
long datalen=0;
-
-
Scanner kbd1 = new Scanner(System.in);
-
-
System.out.println("Enter file name");
-
-
fname = kbd1.next();
-
-
kbd1.nextLine();
-
-
-
fin = new FileInputStream("c:\\data.in");// not to sure how the .txt is found
-
-
while( fin.read()!=-1) {
-
-
datalen++;
-
}
-
-
System.out.println("File "+fname+" is "+datalen+" bytes long");
-
}
-
}
thanks
sandyR
PS is this code right.
Do I need to use PrintWriter or can I use Printf(fname)
A few remarks:
- I don't understand the comment "if 1 is select go to makePerson"
- why are you using a separate Scanner for reading the name of the file? You
already had a Scanner instantiated for the System.in input stream.
- You read the filename "fname" supplied by the user but you don't use it.
You use a String literal for the file name instead.
- If you merely want to know the size of the file there's no need to read it all,
byte by byte. Have a look at what the
File class can do for you.
kind regards,
Jos