Results 1 to 3 of 3
Thread: Working with Files in Java
- 12-29-2009, 09:38 AM #1
Member
- Join Date
- Dec 2009
- Location
- Bangalore, India
- Posts
- 1
- Rep Power
- 0
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.
- 12-29-2009, 10:06 AM #2gcampton Guest
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 }
- 12-30-2009, 08:05 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Regular Expressions make sense if you have a specific patter in text lines.
Similar Threads
-
working with .ini files
By pele in forum Advanced JavaReplies: 0Last Post: 03-27-2009, 09:34 AM -
working with files (text files)
By itaipee in forum New To JavaReplies: 1Last Post: 02-24-2009, 11:38 AM -
Can someone help me working with files?
By Goodwine in forum New To JavaReplies: 4Last Post: 11-18-2008, 04:04 AM -
conversion of java .class files to .java files
By kotturupraveen in forum New To JavaReplies: 2Last Post: 06-09-2008, 12:58 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks