Results 1 to 3 of 3
  1. #1
    Aditya Sriram is offline Member
    Join Date
    Dec 2009
    Location
    Bangalore, India
    Posts
    1
    Rep Power
    0

    Default Working with Files in Java

    Hi all,

    I have been working with java for my latest assignment and I ran into some problems while working with files. I need to access a file, word by word but I also want to know when I moved from one line in the file to the next. I don't want to store the entire line in a String and then access it word by word since the amount of data on a single line may be large. Is there a way for me to access the file word by word and still be able to know when a new Line has occurred?

    I am currently using scanner and the delimiter by default looks for white spaces so I am getting the content word by word, but when it finishes reading a line, it just jumps to the next one.

  2. #2
    gcampton Guest

    Default

    what you can do is read the line by line using hasNextLine(), but then when you grab the line, split it up using split() which gives you a string array of every word. you can use delimeter if you want, but it can be problematic.

    Java Code:
    Scanner inFile = new Scanner(new File("data.txt"));
    String inStr;
    
    while (inFile.hasNextLine())
    {
        inStr = inFile.nextLine();
        String[] splittedString = inStr.split();
    
    // now you have a String[] array called splittedString of the current line
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. working with .ini files
    By pele in forum Advanced Java
    Replies: 0
    Last Post: 03-27-2009, 10:34 AM
  2. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 12:38 PM
  3. Can someone help me working with files?
    By Goodwine in forum New To Java
    Replies: 4
    Last Post: 11-18-2008, 05:04 AM
  4. conversion of java .class files to .java files
    By kotturupraveen in forum New To Java
    Replies: 2
    Last Post: 06-09-2008, 01:58 PM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •