Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem reading text file

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem reading text file

    /*
    Simple program...
    Runs fine no errors.
    Problem, the file has 1440 records of text lines. The program will only read around 400 lines.
    The number of records read vary with each run.
    I have tried other examples but all have the same problem.
    Can anyone solve this ????

    /**
    *
    * @author ubuntu
    */
    import java.io.*;
    import java.util.Scanner;

    public class Test {
    public static void main(String [] args) {
    //String fileName="/home/ubunbtu/NetBeansProjects/CiSentances/src/cisentances/sentences.txt";
    String fileName = "/home/ubuntu/NetBeansProjects/CiSentances/src/cisentances/sentences.txt";
    // The name of the file to open.


    // This will reference one line at a time
    String line = null;
    int count=0;
    try {
    FileInputStream inputStream = new FileInputStream(fileName);
    Scanner sc = new Scanner(inputStream, "UTF-8");

    while (sc.hasNextLine()) {
    line = sc.nextLine();
    /* // FileReader reads text files in the default encoding.
    FileReader fileReader =
    new FileReader(fileName);

    // Always wrap FileReader in BufferedReader.
    BufferedReader bufferedReader =
    new BufferedReader(fileReader);

    while((line = bufferedReader.readLine()) != null) {
    */
    count++;
    System.out.println(count + " " + line);

    }

    // Always close files.
    // bufferedReader.close();
    }
    catch(FileNotFoundException ex) {
    System.out.println(
    "Unable to open file '" +
    fileName + "'");
    }
    catch(IOException ex) {
    System.out.println(
    "Error reading file '"
    + fileName + "'");
    // Or we could just do this:
    // ex.printStackTrace();
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading text file

    Can you explain the purpose of all the I/O classes?
    FileInputStream
    Scanner
    FileReader

    Why not just use one?


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading text file

    Quote Originally Posted by Norm View Post
    Can you explain the purpose of all the I/O classes?
    FileInputStream
    Scanner
    FileReader

    Why not just use one?


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Hi Norm,
    It's been a few years since I last wrote a java program and I'm just going back to how to program in Java in 21 days.
    The code is taken straight from examples on the web. I do not know why I am using a combination of classes.I'm a great cut and paste artist, my goal is more important than the code if it works.
    All I want to do is read 1440 lines of text and stick them in a random file and be able to read a record back in normal characters, after that the rest is easy. Ive at last managed to write to and read from a random file which has taken about 3 days.
    Any Help would be greatly appreciated I cannot believe how much grief I have had for such a simple task.
    Peter

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading text file

    Can you fix the code tags? See my first post.
    Get rid of the dead code.

    I suggest you start with a smaller file to limit what is printed on the screen for debugging.
    When you get the code to work for the small file, try a larger file.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading text file

    Quote Originally Posted by Norm View Post
    Can you fix the code tags? See my first post.
    Get rid of the dead code.

    I suggest you start with a smaller file to limit what is printed on the screen for debugging.
    When you get the code to work for the small file, try a larger file.
    Many Thanks Norm, it's great to find support when you are going crazy over a simple problem.
    I copied and pasted 3 classes I found and the all had the same problem.
    I was working on an old samsung notebook with a 32 bit ubuntu 14.04 and netbeans 8.02.
    I decided to try on my PC upstairs. And You Know... All 3 ran perfectly in 0 seconds.
    Thanks a million for helping , I can now find the next challenge.
    Peter

Similar Threads

  1. reading i n a text file
    By zerocool18 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2014, 04:38 PM
  2. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  3. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  4. problem reading text file to hash map
    By cataschok in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 27th, 2012, 11:41 AM
  5. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM