Java Tutorial/File/BufferedWriter
Содержание
- 1 Appending to a File
- 2 BufferedWriter
- 3 Create BufferedWriter from FileWriter
- 4 Nested creation of FileWriter, BufferedWriter and PrintWriter
- 5 Read Input From User and Write to File
- 6 Use a BufferedReader and a BufferedWriter to copy a text file, inverting the case of letters in the process
- 7 Write to file using a BufferedWriter
- 8 Writing to a File: If the file does not exist, it is automatically created.
Appending to a File
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] argv) throws Exception {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
}
}
BufferedWriter
You can rap your Writer with a BufferedWriter for better performance. BufferedWriter has the following constructors:
public BufferedWriter (Writer writer)
public BufferedWriter (Writer writer, in bufferSize)
Create BufferedWriter from FileWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
public class MainClass {
public static void main(String[] args) throws IOException {
try {
BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
String s = null;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
}
}
Nested creation of FileWriter, BufferedWriter and PrintWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class Main {
public static void main(String[] argv) throws Exception {
FileWriter file = new FileWriter("a.html");
BufferedWriter buffer = new BufferedWriter(file);
PrintWriter out = new PrintWriter(buffer);
out.println("<html>\n\t<body>");
out.println("\t</body>\n</html>");
out.close();
buffer.close();
file.close();
}
}
Read Input From User and Write to File
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws Exception {
InputStream in = System.in;
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
int letter;
while ((letter = in.read()) != -1) {
bw.write((char) letter);
bw.flush();
}
}
}
Use a BufferedReader and a BufferedWriter to copy a text file, inverting the case of letters in the process
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Main {
public static void main(String[] argv) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("in.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
int i;
do {
i = br.read();
if (i != -1) {
if (Character.isLowerCase((char) i))
bw.write(Character.toUpperCase((char) i));
else if (Character.isUpperCase((char) i))
bw.write(Character.toLowerCase((char) i));
else
bw.write((char) i);
}
} while (i != -1);
br.close();
bw.close();
}
}
Write to file using a BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedWriter bufferedWriter = null;
bufferedWriter = new BufferedWriter(new FileWriter("yourFile.txt"));
bufferedWriter.write("string");
bufferedWriter.newLine();
bufferedWriter.write("string");
}
}
Writing to a File: If the file does not exist, it is automatically created.
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] argv) throws Exception {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("asdf");
out.close();
}
}