How to input and output from/or to a file?!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster

How to input and output from/or to a file?!

 
0
  #1
34 Days Ago
Hi mates..

I'm trying to find a way in how to inputing or outputting from a file in Java.

For my bad luck when I finished my Java course, our professor didn't talked about this thing.

I searched in the internet, and found a way in inputing from a file which is:
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. try {
  4. Scanner input = new Scanner(new File("file.txt"));
  5. } catch (FileNotFoundException e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. }

but after that, how to read the content of that file! also, if the file has a paragraph how can I search for a specific words, I think in java I can only read a line not a word!

for example "a" or "if" or "playing" how can I look for them if they exist in the file or not.

After that, how to output the data I want in an output file!


Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
0
  #2
34 Days Ago
First of all check the Scanner API. You found a code and you didn't think to check the API.

Now, there is the nextLine method that reads the next line of the file. Combine it with the has.. methods to read the entire file:
  1. while (scanner.hasNextLine() ) {
  2. String line = scanner.nextLine();
  3.  
  4. // check if the line contains the words you want
  5. int i = line.indexOf("if");
  6. // if i==-1 then not found
  7. }
You can store the entire file in a Vector(add each line to a vector as the loop runs) and then loop through the vector to find the words you are looking for. Or you can check the line as you read it.
You might want to use the method: String.indexOf(String s)
It will return -1 if the argument is not found (check the String API)


OR
For a more sophisticated way, use regular expressions and patterns with these methods:
http://java.sun.com/j2se/1.5.0/docs/...a.lang.String)
or
findInLine
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #3
34 Days Ago
If you want to find a specific word from within a file, another way to do it would be to use Scanner's next() method, then use String.equals to see if they match.
Out.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
0
  #4
34 Days Ago
Originally Posted by BestJewSinceJC View Post
If you want to find a specific word from within a file, another way to do it would be to use Scanner's next() method, then use String.equals to see if they match.
Actually my solution is not correct. If a word contains the argument then it will return that it found it, even though it is not a whole word but only a part of it.

In order for my solution to work, the OP would have to use the .split(" ") method to "break" the line and then use the equals method the way you described.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #5
34 Days Ago
Thanks mates for those useful links.

Sorry for that but I got confused, so which solution you think it'll be easier to implement and as you said javaAddict I want to find the WHOLE word if exist not a part of it.

like for example when I search for "ok" it wouldn't take this as a found "okay".

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #6
34 Days Ago
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html

Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop

  1.  
  2. while(scanner.hasNext()) System.out.println(scanner.next());

And just look at what it prints to answer your question.
Out.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #7
33 Days Ago
Originally Posted by BestJewSinceJC View Post
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html

Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop

  1.  
  2. while(scanner.hasNext()) System.out.println(scanner.next());

And just look at what it prints to answer your question.
WOW, thank you so much I'm now coding easily

one more thing please, if I want to check a specific word where it occurred in which line of the file, how to do that?

I mean for example "a" I want to see in which line it appears, how to do that!?


Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #8
33 Days Ago
This is my code now, WHY i'm getting an error!!

Here is the Data.java:

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class Data {
  6.  
  7. private String[] a = {"a", "if", "is", "of", "peck", "peppers", "peter", "picked", "pickled", "piper", "that", "the", "where"};
  8. private int Freq[] = new int[50];
  9.  
  10. public int[] getFreq() {
  11. return Freq;
  12. }
  13. public void setFreq(int[] f) {
  14. Freq = f;
  15. }
  16.  
  17. public void searchForFreq()
  18. {
  19. Scanner input;
  20. try {
  21. input = new Scanner(new File("file.txt"));
  22. } catch (FileNotFoundException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. while( input.hasNext() )
  27. {
  28. if( input.next() == "a")
  29. Freq[0] = Freq[0] + 1;
  30. else
  31. if( input.next() == "if")
  32. Freq[1] = Freq[1] + 1;
  33. else
  34. if( input.next() == "is")
  35. Freq[2] = Freq[2] + 1;
  36. else
  37. if( input.next() == "of")
  38. Freq[3] = Freq[3] + 1;
  39. else
  40. if( input.next() == "peck")
  41. Freq[4] = Freq[4] + 1;
  42. else
  43. if( input.next() == "peppers")
  44. Freq[5] = Freq[5] + 1;
  45. else
  46. if( input.next() == "peter")
  47. Freq[6] = Freq[6] + 1;
  48. else
  49. if( input.next() == "picked")
  50. Freq[7] = Freq[7] + 1;
  51. else
  52. if( input.next() == "pickled")
  53. Freq[8] = Freq[8] + 1;
  54. else
  55. if( input.next() == "piper")
  56. Freq[9] = Freq[9] + 1;
  57. else
  58. if( input.next() == "that")
  59. Freq[10] = Freq[10] + 1;
  60. else
  61. if( input.next() == "the")
  62. Freq[11] = Freq[11] + 1;
  63. else
  64. if( input.next() == "where")
  65. Freq[12] = Freq[12] + 1;
  66. }
  67. for( int i = 0; i < Freq.length; i++ )
  68. {
  69. System.out.println( a[i] + "...." + Freq[i]);
  70. }
  71.  
  72. }
  73.  
  74.  
  75. }

and this is Driver.java:

  1. public class Driver {
  2.  
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8.  
  9. Data d = new Data();
  10. d.searchForFreq();
  11.  
  12.  
  13.  
  14. }
  15.  
  16. }


any clue on how to solve the error?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso
 
0
  #9
33 Days Ago
First of all run this code:
  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }
The name of the method is pretty self explanatory:
Whenever you call it it returns the next word.
In your while loop you keep calling it again and again inside the loop so every time you get the next word. This doesn't compare the same word:
  1. if( input.next() == "a") // this will return a word
  2. Freq[0] = Freq[0] + 1;
  3. else
  4. if( input.next() == "if") // this will return a different word from the previous one
  5. Freq[1] = Freq[1] + 1;

Also use the equals method to compare objects such as the String object:
if ( s.equals("a") ) { .. }
Last edited by javaAddict; 33 Days Ago at 9:09 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 130
Reputation: Q8iEnG is an unknown quantity at this point 
Solved Threads: 2
Q8iEnG Q8iEnG is offline Offline
Junior Poster
 
0
  #10
33 Days Ago
Originally Posted by javaAddict View Post
First of all run this code:
  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }
The name of the method is pretty self explanatory:
Whenever you call it it returns the next word.
In your while loop you keep calling it again and again inside the loop so every time you get the next word. This doesn't compare the same word:
  1. if( input.next() == "a") // this will return a word
  2. Freq[0] = Freq[0] + 1;
  3. else
  4. if( input.next() == "if") // this will return a different word from the previous one
  5. Freq[1] = Freq[1] + 1;

Also use the equals method to compare objects such as the String object:
if ( s.equals("a") ) { .. }
ahaaa omg i messed around lol

So, you mean I should first save the input in a variable and compare it
like this:
  1. String s = input.next();
  2. if( s.equals("a") )
  3. blah blah
  4. else if( s.equals("if") )
  5. blah blah
  6. ...

That is what you meant?

And yeah I ran this code:

  1. while(scanner.hasNext()) {
  2. System.out.println(scanner.next());
  3. }

and it worked perfectly.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC