How to work with files? Hacks for beginners
-
What does it mean "file" or "usage of files"?
Working with files means reading from a file and outputting into a file, not working with the console.Can you work with the console and files at the same time?
Of course, you can.Usually, the files are in txt format, so make two files "input" and "output" (or every name you like), better in the folder of project, otherwise you need to write then the direction of file.
And here are the lifehacks how to deal with them:-
There can be a mistake while trying to read your file. Make sure you've written file "location" correctly.
-
Moreover, if you are sure, there will be no mistakes, USE
try&catch, otherwise your program is not correct.
Here is the example:
try (...) { ... } catch (IOException e) { e.printStackTrace(); }
- Better to work with BufferedReader.
String inputFileName = "input.txt"; try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) { ... } catch (IOException e) { e.printStackTrace(); }
And so with a writer: work with FileWriter
try(FileWriter writer = new FileWriter("output.txt", false) { ... catch(IOException ex){ System.out.println(ex.getMessage()); }
-
If you are writing into a file during "for" or "while", remember to start your try before.
-
So if you work with line reading, you have more opportunities than with symbol reading. There are more and better functions for string.
-
You can use this try for writing once. Why? Because you close try and then start a new one, so... you start writing into your file like for the first time.
-
How do we read with lines?
while ((line = reader.readLine()) != null) {}
While your line (a simple string) is not empty. It takes a line from a file every time until it is empty, so you work with lines.
- How do we write?
writer.write(line); writer.flush();
Remember, that numeric symbols should be converted into string before writing
That is a small help for all beginners, that want to get accouanted with files, everything else is the same. If you need some help, you can write me anytime.Good luck in your job!
modedit removed link
-
-
@Lionking202084 I wouldn't exactly call coding file I/O a "Life hack". Lifehacks are supposed to mean things you do to life in general to save time and effort, not coding tips. But OK, maybe just call it a "hack" then?
Probably should mark this as spam though? First poster, link to business.
-
@Pathduck i can delete that link. I don't need a promotion, I need to find people who are also intersted in it to talk with them. And yes, it is my first article, I'm new at posting, but I want to get this skill, sorry
-
@Pathduck maybe I've written that not correclty, but I thought it would be useful for those who want to make first step
-
@Lionking202084 I recommend you writing stuff like this on your blog instead of the forum — as you see, here it’s observed critically, but on the blog it’s just… normal. People write there whatever they want & it’s also what the blogs are for.
-
@potmeklecbohdan yes, I tried to write on some communities like dev.to and some others ,there it goes fine... maybe you know some more pouplar communities? I want to communicate, so I can't write only in my blog, cause it is a blog for company, so I can't find anyone to discuss with. Thanks for advice!