hello guyz,,do you know best sites to recommend on :
tutorials about reading a file ("text file") and writing it in "java file??
in short
reading or writing files in bufferedreader..

thank you ahead guyzz..

Dani AI

Generated

For reading and writing text files in Java there are two practical patterns worth knowing: the modern java.nio.file APIs (preferred for clarity and charset control) and the classic java.io approach with BufferedReader/BufferedWriter. , and pointed to good starting examples; those are useful, but many simple examples miss important details like explicit charset handling, safe resource closing, and how to avoid loading very large files into memory. The snippets below show safe, minimal patterns that work on Java 7+.

Path input = Paths.get("input.txt");
try (BufferedReader br = Files.newBufferedReader(input, StandardCharsets.UTF_8)) {
    String line;
    while ((line = br.readLine()) != null) {
        // handle the line (readLine() strips the line terminator)
    }
}

Use Files.newBufferedReader so the Path + Charset are handled together, and use try-with-resources so the reader is always closed. For streaming processing of very large files consider Files.lines(path, charset) which returns a Stream<String> (remember to close it or use try-with-resources around the stream).

Path out = Paths.get("output.txt");
try (BufferedWriter bw = Files.newBufferedWriter(out, StandardCharsets.UTF_8,
        StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
    bw.write("some text");
    bw.newLine();
}

Files.newBufferedWriter gives control over encoding and open options. For small content Files.write or, in newer JDKs, Files.writeString are concise alternatives.

Practical cautions: always specify a charset (avoid platform default), don't use readAllLines on huge files, confirm the working directory or use absolute paths to avoid FileNotFound errors, and handle IOException. For atomic updates write to a temp file and Files.move into place. Finally, follow 's advice: study each line rather than copy/paste — understanding charsets, buffering, and resource management avoids the common runtime bugs.

Recommended Answers

All 3 Replies

Bare-bones example:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.