Results 1 to 20 of 24
Thread: Reading a text file
- 03-10-2012, 09:39 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
- 03-10-2012, 10:46 PM #2
Re: Reading a text file
Read the file, line by line. Find the word and copy it to a String variable.
How will you identify the location and length of the word?
If you already know what you are going to find in the file, what is the purpose of reading the file?
- 03-11-2012, 02:12 AM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Sorry let me re-phrase the question. Well what im trying to do is read a text file and to find the line on which the word is, and its ID number. For an example below; where this may be contained in the text file, and say I wanted to find the PID by providing 0.0.0.0:135 to search it by. Where the program should then return 1012. And sorry, not copy it as a String variable but an integer variable and to pass it into a method?
Thanks. Hope you can understand where i'm comming from?
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1012
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
- 03-11-2012, 02:15 AM #4
Re: Reading a text file
Is this what you are trying to do:
Find a line with a specific String and then extract another String from another position on that line.
- 03-11-2012, 02:26 AM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Yes exactly, or rather find the ID number on the same line as the searched String given?
Thanks
- 03-11-2012, 02:29 AM #6
Re: Reading a text file
The String class has methods for detecting if a String contains another String and for getting substrings out of a String.
- 03-11-2012, 02:34 AM #7
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Ok. But how do I scan the text file in order to search for a specific String? I am aware of the methods that String supplies.
Thanks
- 03-11-2012, 02:36 AM #8
Re: Reading a text file
I know so far how to read a text fileThese are confusing.how do I scan the text file
Can you explain what these two statements mean?
If you know how to read the lines of a text file into a String,
that is all there would be to do to scan a text file. Read a line and scan it.
-
Re: Reading a text file
Work off of the fact that each field is separated by a whitespace, so you can use the String.split() method to get an array of Strings from each record.
Example:
Now that you know how to find the values, you should store them in a hashmap to get the values easilyJava Code:String line1 = "TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1012" String[] fields = line1.split(" "); System.out.println(fields[0]); //output: TCP System.out.println(fields[1]); //output: 0.0.0.0:135 System.out.println(fields[2]); //output: 0.0.0.0:0 System.out.println(fields[3]); //output: LISTENING System.out.println(fields[4]); //output: 1012
Now you can get the value anytime like this:Java Code:private final int KEY_FADDR = 1; private final int KEY_PID = 4; //keep track of all PIDs Map<String,Integer> myPIDs = new HashMap<String,Integer>(); //loop through each line in the text file for (String line:String[] textfile) { String[] fields = line.split(" "); String foreignAddr = fields[KEY_FADDR]; int pid = Integer.valueOf(fields[KEY_PID]); myPIDs.put(foreignAddr, pid); }
Java Code:int pid = myPIDs.get("0.0.0.0:135"); //pid = 1012Last edited by ozzyman; 03-11-2012 at 02:51 AM.
- 03-11-2012, 02:47 AM #10
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Oh yeah, sorry for my confusing grammer lol. What i mean is i know how to create a FileInputSream object containing the text file and streaming the file into the program using InputStreamReader. So therefore I would like to return the ID number contained in the file and pass it onto another method. All this would be in run-time where id would input a string and return the number along that line under PID. Hope this makes sence?
Thanks
- 03-11-2012, 02:56 AM #11
Re: Reading a text file
Read the file line by line.
test if the line contains the String.
Get the substring
convert the String to an int
return the int
-
Re: Reading a text file
If you want to pass it onto another method its pretty simple with the example I gave you
Java Code:public int getPID(String foreignAddr) { return myPIDs.get(foreignAddr); }
- 03-11-2012, 03:01 AM #13
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Ok thanks alot, this looks very promising. Il try this out see how it works.
Thanks again
- 03-11-2012, 09:29 PM #14
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
- 03-11-2012, 09:32 PM #15
Re: Reading a text file
Post the full text of the error messages.
-
Re: Reading a text file
No you were supposed to do that bit yourself... Read a text file into a Collection<String> or String Array (whichever you prefer) and then pass the field into the for loop.
It was more of a pseudo code/example code for you than actual code.
I thought you said you're find with reading the text file anyway?Last edited by ozzyman; 03-11-2012 at 09:36 PM.
- 03-12-2012, 03:57 PM #17
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
Oh yea I have read in the textfile.txt into an array. Ive got one problem, its giving me an error NumberFormatException, which I think its trying to convert the String into an integer, and the first 4 lines do not contain numbers until the 5th line, which is shown below:
Active Connections
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 508
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:554 0.0.0.0:0 LISTENING 5856
The 'Active Connections' is on the second line, the 3rd is an empty line, and the 4th line containes the headings 'Proto Local Address Foreign Address State PID'. Where the 5th line then starts with the PID's etc.
So would I have to tell the for loop to start looping from the 5th index of the textfile array? Or how would I get around this problem?
Thanks
Im getting the error from this line: int pid = Integer.valueOf(fields[KEY_PID]);
- 03-12-2012, 04:03 PM #18
Re: Reading a text file
What is in: fields[KEY_PID]?
For debugging use the Arrays toString() method to format the contents of an array for printing.
If you know which line has the number, skip the preceding lines. You could have a loop that reads and ignores lines before the lines with the numbers.the first 4 lines do not contain numbers until the 5th line
- 03-12-2012, 04:27 PM #19
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Reading a text file
The process ID numbers, which is in the 4th field (column) in each line. KEY_PID is assigned to 4.
Use Arrays to String() method to format the contents of an array for printing. Would can I do this within the following code below?
Although when I ran it, the textfile looked the same as before. Maybe I doing it the wrong way?Java Code:while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println (strLine); for(int i=0; i<textfile.length; i++) { //assign each line to to textfile textfile[i] = br.readLine(); //Trying to format the textfile? System.out.println(textfile[i].toString()); } }Last edited by Norm; 03-12-2012 at 04:33 PM. Reason: added code tags
- 03-12-2012, 04:32 PM #20
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
Reading in a text file
By TheRealHoff in forum AWT / SwingReplies: 10Last Post: 02-07-2010, 11:47 PM -
reading text file
By trofyscarz in forum New To JavaReplies: 1Last Post: 02-05-2010, 02:24 AM -
Reading two text file and sum them up
By matt_well in forum New To JavaReplies: 36Last Post: 07-22-2008, 02:55 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks