need some understanding here on what my objective is in this assignment. I'm not looking for anyone to write my code for me. I'm still a noob in coding, and don't quite understand the lingo as good. However, I will mention what I thought of so far and hopefully I'm right.
The assignment:
"Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the sentinel string ("DONE") to the output file."
I can't copy/paste pics yet since I'm also new to Stack Overflow.
The results however show this in the IDE's terminal:
A string.
Another string.
Yet more text...
DONE
Then right next to that pic is showing text but in notepad:
A string.
Another string.
Yet more text...
So my conflictions lie in "Write a program that reads strings from the user.."
which made me think that I have to write a scanner that reads lines from a user input on the IDE terminal and the program stores it into userStrings.txt.
That seems excessively hard from my point of view compared to an assignment that was 30 points dealing with inheritance and classes; and this only being worth 15 points seems to be asking for a lot.
After I studied and a little bit and search good'o google I found that people mainly program either filewriter (which writes a directly into notepad) or a filereader (that reads a current file in notepad).
So which one is it?
Scanner + filewriter?
Just a plain file writer or reader?
Literally losing my mind lol. Also any brief elaboration is helpful too, in noobish please.
Appreciate the help!
Sounds like you've got the right answer in using the Scanner class. I'm sure FileWriter would work to write to file, but there's a class PrintWriter which is basically the output version of Scanner. Both Scanner and PrintWriter have methods that are pretty easy to understand, so I'd go with those two.
Once you have those set up, it's shouldn't be too difficult to read in a string from Scanner, pass it PrintWriter, and then write that to a file.
Good luck on the homework! :)
Related
I was looking back at some older class assignments and for one the user had to provide a text file which would be encoded according to an encryption key the user also gave. I essentially solved the problem by placing the content of the text file within a string, retrieving each letter from the string and encrypting it, and then printing the encrypted character back into the same text file. The problem is my professor docked 5% for storing the whole file content within a string, writing something like: "What if the file contents were very large?" I even recall the few people I talked to after the project was graded saying they lost points for the same reason.
At the time I thought he made sense and was too overburdened by my workload so didn't bother seeing if I could fix it because it seemed to be reasonable and simple enough. However now I can't understand how one would be able to edit the text file directly or write on the same file without storing the entire string (because one would otherwise lose its content). How would someone even go about this? Thank you!
Edit: whomever marked my thread as a duplicate to the one above clearly did not understand my question. I am asking how to manipulate the same file without using an absurd amount of memory as the solution I stated would. The other thread clearly asks what the quickest way to read from a file is, which is not at all the same thing. Joop had the right idea of what I meant so I'll try just that, thank you Joop.
I'm new to programming, so I was wondering if someone could help me. What I'm trying to is make the user input a number, and if the number is in the file, it'll spit something back out, and if it's not in the file, it'll keep asking for a number. I don't know exactly how to do this, but if someone could point me in the right direction, that would be great. Thanks in advance!
First read the file and store it (as a Collection, or as a big
String, the exact type depends on how you actually trying to
achieve this).
Reading a file can be done in numerous ways, some of them are using a Scanner, BufferedReader or File.readAllLines()
Now, create a while loop - that repeatidly asks the user for a
number, until the condition that the number is in your data is met.
Next, you need to process and yield the desired output.
Bonus:
Try to do each of the bullets in a different method and only combine them in your main() - it will help you to learn about basic methods design as well.
This is my understanding regarding reading a file using BufferedReader in java. Please correct me if I am wrong somewhere...
Recently I had a requirement where we are required to read a file multiple times.
The usual way which I use is setting a mark() and doing a reset. But the input parameters to
a mark is an integer and it cannot accept a long number. Is there a way in which we can read the file, a large number of times.
In c++ we can do a seekg on the fstream and read the contents once again irrespective of the number of times we want to do so. Is there anything in java which is of this nature.
Just close the file and read it again.
But review your requirement. Why can't you process it in one pass?
Not much of a good answer but if you want to do random reading and writing then you can use Channels in java.nio package.
BufferedReader is for reading a file when you logically see it as a series of records and records are generally accessed sequentially.
Channels allow you to view your file as a series of blocks. Blocks are meant to be read randomly. :)
Using subclass of channel, FileChannel, you can read what you want from wherever you want. You need to specify two things:
Where to read from.
How much to read.
It has a read(dst,pstn) where dst is a ByteBuffer and pstn is a long position.
Don't worry that it is abstract because you use it via Files.newByteChannel() which does all the voodoo needed to make it work :)
I've read some posts on stackoverflow about this topic but I'm still confused. When reading a file that is currently being written in Java, how do you keep track of how many lines have actually been written so that you don't get weird read results?
EDIT: sorry, I should have mentioned that the file writing it is in C++ and the one reading it is in Java so variables can't really be shared easily
When reading a file that is currently being written in Java, how do you keep track of how many lines have actually been written so that you don't get weird read results?
The problem is that you can never be sure that the current last character of the file is the end of a line. If it is a line terminator, you are OK. If BufferedReader.readLine() will interpret it as a complete line without a line terminator ... and weird results will ensue.
What you need to do is to implement your own line buffering. When you get an EOF you wait until the file grows some more and then resume reading the line.
Alternatively, if you are using Java 7 or later, the file watcher APIs allow you to watch for file writes without polling the file's size.
By the way, there is an Apache commons class that is designed for doing this kind of thing:
http://commons.apache.org/io/api-2.0/org/apache/commons/io/input/Tailer.html
If I understand, the file is being written in C# in some process and another Java process wants to read it while it is being written.
Look at File Monitoring section on the tail command here. But I want to warn you that when I used the cygwin tail on Windows recently to follow log files that were rolling over, it sometimes failed under heavy load. Other implementations may be more robust.
To have a count of the number of lines, just keep a counter on the side that's doing the writing.
So, every time you write a line, increment a counter, and make that counter readable via a method, something like, public int getNumLinesWritten()
The obvious answer to me... Why not use a buffer? Use a string or whatever you need. (You could use a list/array of strings if you want, one for each line maybe?) Append to the string just as you would write to the file, then instead of reading from the file, read from that string. Would that work for you?
I hope there do have an operation for this topic,'cause I don't want to loop the file once again,and hope to read the file from the specific location say a line number,and then I will read the file with much more threads than just one.
Any idea?
Thanks first!!
To my knowledge there isn't anything like this in the standard Java API. You could use LineIterator or (even just a basic BufferedReader) to build a custom class that does what you need, like this guy did.
Note that a RandomAccessFile sounds promising but unfortunately for you, the seek() method takes an offset in bytes and not in lines so unless your lines are all always the same length, this wont' work for you.