Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

Java Reader and Write Files

Example Application: Reader (FileReader) and Write (FileWriter) files.

Read File

Imports

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.IOException;

Check file exists

⚠️ Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

Filefile = newFile(FILE);
if (file.exists()) {
//TODO
} else {
//TODO
}

Read file

⚠️Need add throws declaration or surround with try/catch;

StringFILE = "YOUR_LOG.log";
FileReaderreader = newFileReader(FILE);
BufferedReaderbuffer = newBufferedReader(reader);
Stringline = null;
while ((line = buffer.readLine()) != null) {
//TODOSystem.out.printf("%s\n", line);
}

⚠️ Closes the stream and releases any system resources associated withit.

buffer.close();

See the implementation: project

Write File

Imports

importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;

Check file exists

⚠️ Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

StringFILE = "YOUR_LOG.log";
Filefile = newFile(FILE);
if (file.exists()) {
//TODO
} else {
//TODO
}

Write File

⚠️Need add throws declaration or surround with try/catch;

Stringcontent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
StringFILE = "YOUR_LOG.log";
FileWriterwriter = newFileWriter(FILE);
BufferedWriterbuffer = newBufferedWriter(writer);
buffer.write(content);
System.out.println("Success! You file is saved!");

⚠️ Closes the stream and releases any system resources associated withit.

buffer.close();

See the implementation: project

Exceptions

  • IOException

Some links for more in depth learning

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages