TRex22's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
1 (0 per day)
Joined:
04-September 12
Profile Views:
823
Last Active:
User is offline Sep 06 2012 06:43 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   TRex22 has not set their status

Posts I've Made

  1. In Topic: Reading and writing to a file in java

    Posted 4 Sep 2012

    My IT teacher prefers to use a scanner to read a text file and then a loop with another scanner to read each line of the text file until the end of the file. This proves to have better data validation, error checking but is perhaps a bit longer in code and the Scanner class in Java is notorious from some strange errors. It also allows for delimeters and as far as I know it is easier in that respect to a read buffer.

    Scanner scFile = new Scanner (new File("helloworld.txt")); //create the first scanner and use I/O class to open the file
    while(scFile.hasNext())//run the loop to the end of the file
    {
    //use a scanner for a new line and also can have a delimeter string
      Scanner scLine = new Scanner (scFile.nextLine()).useDelimeter("#");
    //From here you can use local variables or fields with the scLine.next() or .nextInt() ect... to save each line
    //or variable from the file
    //close scLine stream
      scLine.close();
    }
    //close the Scanner stream
    scFile.close;
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

TRex22 hasn't added any friends yet.

Comments

TRex22 has no profile comments yet. Why not say hello?