• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read from file, write to file  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


First method reads text from the file.
Second method should write text to the file.

The idea is to collect data from various files ( here I used only 1 ),
and write them to one. Yet 14th line drops mistake.


For example:
File1 contains 100 words, I pick up 10 words from it.
File 2 contains 50 words, I pick up 5 words from it.
10+5. I want those 15 words to write to new file.

( Please note: File1 and File2 holds 10 words in each line )
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Yet 14th line drops mistake.


Well, simply, void methods return ... nothing. There isn't anything to return. And hence, there isn't anything that can be passed as a parameter to another method.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your workers() method reads in lines but your requirements talk about words. The workers() method, as it reads lines, will print the line out but does not retain the line(s) in order to do something useful in result(). The result() method opens a file, prints a String, then closes the file. What if you have multiple Strings to write to the file?
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes. As you can see.
So If I put return inside 'while', it returns only first line of my text file. my text file contains 3 lines.

if I place 'return read' outside 'while', it is not recognized, because of {}.

Text file:
Bob collect bricks.
Rammer chop wood
Ram carpeting


Console output:
Bob collect bricks.
Bob collect bricks.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is that this is what it looks (to me) what you want to do:

but what you've written does:


Now, the second one is doable if you read all the lines into a List<String> (there's actually a method for that) and then iterate over that calling the write method.
But at the moment you;re falling between two stools.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This time, i want to read all all lines from the text file, and write them to other text file.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The issue is that this ... is what you want to do:
...
but what you've written does:
...

...at the moment you;re falling between two stools.

In other words, your program is as incoherent as a drunk coal miner on payday.

By "incoherent" I mean that there's a disconnect between what you think you're telling Java to do and what you're actually telling Java to do.

Remember, commands are executed sequentially. One or more commands can be repeated in a loop, like in that for-loop to read lines from the first file. Inside the loop, commands are still executed one after another in the order that they appear. No other commands are executed except for the ones in the loop. The only thing that the code in that first loop does is exactly what you've written: line 33 reads a line from the first file and line 34 prints whatever was read. The program execution won't suddenly jump down to line 45 just because that's what you intended for it to do. 

Again, as Dave was saying, the problem with your first for-loop is that you're not doing anything to remember any of the lines you've read from the file. On line 34, you're just printing them to System.out.  System.out doesn't have the capability to remember output that you want to use later on in the program. To remember the lines that were read, you'll need to store them somewhere, like a List<String> as Dave suggests.  That list of strings is what you'd pass to the other method that writes lines to the second file.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have updated.
Now It scans file and prints it to another file. However I expected to be like this:

Bob collect bricks.
Rammer chop wood
Ram carpeting

But I got:
[Bob, collect, bricks., Rammer, chop, wood, Ram, carpeting]


Also I had add 3rd method, who writes some text to file. Why 3rd method deletes everything what 2nd have done?
Also any simple recommendation/example how to scan text from file A and write it to file B ?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Why 3rd method deletes everything what 2nd have done?


A good place to start is the JavaDoc for the PrintWriter class... specifically... this

Henry
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner.next() will read one word (technically it reads the next token which by default is defined as a sequence of characters that does not include any whitespace).  Given that, your code does not read lines, it reads separate words and adds each one to your list of String.  Also, by passing the list to System.out.println instead of each thing that you read from the scanner, you will essentially print out the value of the list's toString() method, which is why you see the output you're getting.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
bob john wrote:
Why 3rd method deletes everything what 2nd have done?


A good place to start is the JavaDoc for the PrintWriter class... specifically... this

Henry

My most nonVisited java site. Because I dont understand anything in manner it writes.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
My most nonVisited java site. Because I dont understand anything in manner it writes.


Well, since that is the official documentation, it is probably not a good idea to avoid that site.  That is like a writer / author that decided to not use the dictionary.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing, from the code you just posted.
Line 13 isn't needed.  No sense reading in the data twice.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it difficult to read your code. Probably because class/variable names are misleading and mean different thing than you'd naturally expect them to mean.

For instance Counting class, which is not doing any mathematical operations, so what is it doing? Reading file and writing data to somewhere else. What it has to do with counting? 
I understand it is for practice, but you'll be surprised how difficult it is to train good habits to choose right variable names. It is not even a habit, but rather a skill, which takes time to build. And since you don't practice it, I guess you'll be tend to cut corners in every other practice exercise, later assignment, and probably in the end at work - which is not a good thing.

Another thing - if you use Scanner, don't use File along with it. Oracle warns about unexpected effects in some circumstances. There is a new Path object (since Java 1.7), and its use is next:
And since Java 8 there are more ways of reading and writing files - these look simpler. Some of those involve Stream API. Have a look, you might will find it more convenient.

Indentation not much improved improved comparing to other your written code. It is another bad habit, which does no good even during short practices - it is just wrong. Not sure why you doing that.

There are other bad practices you use and been told not to use already.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!