• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write and read via ArrayList with txt file?  RSS feed

 
Emil Nie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanna learn to create a method where I use a scanner or any other way to input.
The stuffs I input will be strings. The whole thing will be like a destination creator.

So if I input the string USA, it will write this to a file:

1:USA

When I go to the method again the ID will be added so if I input UK, the file will contain:
1:USA
2:UK

Now if I terminate the program, next time when I run it again it will be able to read the ArrayList, so if I wanna go to the method again and write: Norway
the file will contain:

1:USA
2:UK
3:Norway


Later I also want to be able to use println to show all of these lines from the file, as well I would like to be able to delete an ID, so if I delete ID 2 the file would contain:
1:USA
3:Norway


I wonder what write and read technique is best to use?
Is there any great video tutorials that you can recommend me?
I've some problem in learning by reading and I'm quite new to Java, I'm in a Java course since soon 3 weeks back and I have sure learned a lot, all the loops and basic statements that exist in most languages, but now when it comes to this I just don't understand. I understand the scanner but not really how to write to the file and be able to read it and fix this with the ID's so it continues from the very last ID.
Thanks in advance
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Emil.

That sounds like a good exercise to learn some useful Java programming techniques. Oracle has many useful tutorials for Java, here are some that will be useful for your idea:

Scanning - how to use java.util.Scanner

Character streams - how to use a Writer and a Reader to write to or read from files

Collections - how to use collection classes such as ArrayList
 
Emil Nie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but do know any recommended video tutorial or similar?
I wish they had explained the code better, now I tried build some but without really using the array cus I don't understand at all.

fileDest is already declared and the main is on another class but it calls this class.






Nothing prints. Tried with .write too
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emil Nie wrote:

Line 10: First time run you'll get a FileNotFoundException. No exception if you create the file before hand.
Line 14: Note - throws away the new line character.
Line 17: Not adding the new-line back in.
Line 20: Reading a single character and throwing it away - you aren't even printing it.
I've never tried having the same file open for both reading and writing at the same time so I'm not sure of the behavior.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried looking up the API's of the classes and methods that you are using?
Java API doc
 
Emil Nie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's lots of API's..


Why doesn't this work?
I'm reading from the Scanner class: http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

I thought if best idea might be to use scanner for input to file as well?
Tried stuffs like

But it doesn't work.
Error just says error syntax.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because you are not writing round brackets () after the call to the File constructor. The compiler will never be happy with that sort of code.
You will have to learn the syntax for passing arguments to a constructor. It looks just the same as passing arguments to a method.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try reading and writing on the same file. When you open the file to read, the OS will lock it so you can't write. When you try to open a locked file, you will get a FileNotFoundException.
You need to read from one file and write to a different file.
You also must close readers and writers (except those pointing to System.in, System.out and System.err). Otherwise you are risking a resource leak and failing to write.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use a Scanner for reading and you use a Formatter for writing. Look at this Java® Tutorials section.
 
Emil Nie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Emil Nie wrote:

Line 10: First time run you'll get a FileNotFoundException. No exception if you create the file before hand.
Line 14: Note - throws away the new line character.
Line 17: Not adding the new-line back in.
Line 20: Reading a single character and throwing it away - you aren't even printing it.
I've never tried having the same file open for both reading and writing at the same time so I'm not sure of the behavior.



Whatever I will use this FileWriter.
But I don't understand anything what you say.
Line 10, I already have the file created, and the file will be created if it doesn't exist of the tutorials I been gone through.
Line 14 not understood, line 17 not understood, line 20 not understood, of what you mean.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which tutorial have you been through? You would appear not to have followed all of it, or maybe not read all of it. you should not use file readers and file writers on their own, but their buffered counterparts or Scanners and Formatters.
You have some confusing variable names: output for your input and input for your output.
You are reading with nextLine and not adding any new line sequences, so your output file will contain all its output as a single line. That is feasible, but will be illegible if you display the contents of the file. Only there is a good chance the the contents will remain in the buffer and will never be written to your file at all.
And why on earth have you got the read() call on line 20?

Have you actually read CB's and my replies?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!