Java by API/java.io/PrintWriter
Содержание
- 1 new PipedWriter()
- 2 new PrintWriter(BufferedWriter, boolean autoFlush)
- 3 new PrintWriter(Writer out)
- 4 PrintWriter: close()
- 5 PrintWriter: print(char c)
- 6 PrintWriter: print(int i)
- 7 PrintWriter: println()
- 8 PrintWriter: println(char x)
- 9 PrintWriter: println(double x)
- 10 PrintWriter: println(float x)
- 11 PrintWriter: println(int x)
- 12 PrintWriter: println(long x)
- 13 PrintWriter: println(Object x)
- 14 PrintWriter: println(String x)
- 15 Writer: write(char[] cbuf)
new PipedWriter()
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
public class Main {
public static void main(String[] args) throws IOException {
PipedWriter pw = new PipedWriter();
PipedReader pr = new PipedReader(pw);
int ch;
try {
for (int i = 0; i < 15; i++)
pw.write(" A" + i + "\n");
while ((ch = pr.read()) != -1)
System.out.print((char) ch);
} catch (IOException e) {
}
}
}
new PrintWriter(BufferedWriter, boolean autoFlush)
/*
* Output:
*
*/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class MainClass {
public static void main(String args[]) {
try {
// Create a print writer
FileWriter fw = new FileWriter(args[0]);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw, false);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
new PrintWriter(Writer out)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
try {
BufferedReader input = new BufferedReader(new FileReader(args[0]));
ArrayList list = new ArrayList();
String line;
while ((line = input.readLine()) != null) {
list.add(line);
}
input.close();
Collections.reverse(list);
PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
for (Iterator i = list.iterator(); i.hasNext();) {
output.println((String) i.next());
}
output.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
PrintWriter: close()
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
try {
char[] chars = new char[2];
chars[0] = "\u4F60";
chars[1] = "\u597D";
String encoding = "GB18030";
File textFile = new File("C:\\temp\\myFile.txt");
PrintWriter writer = new PrintWriter(textFile,encoding);
writer.write(chars);
writer.close();
// read back
InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
char[] chars2 = new char[2];
reader.read(chars2);
System.out.print(chars2[0]);
System.out.print(chars2[1]);
reader.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
PrintWriter: print(char c)
import java.io.FileWriter;
import java.io.PrintWriter;
public class Main {
public static void main() throws Exception{
PrintWriter pw = new PrintWriter(new FileWriter("dice.txt"));
for (int i = 1; i <= 1000; i++) {
int die = (int) (1 + 6 * Math.random());
pw.print(die);
pw.print(" ");
if (i % 20 == 0)
pw.println();
}
pw.println();
pw.close(); // Without this, the output file may be empty
}
}
PrintWriter: print(int i)
import java.io.FileWriter;
import java.io.PrintWriter;
public class Main {
public static void main() throws Exception{
PrintWriter pw = new PrintWriter(new FileWriter("dice.txt"));
for (int i = 1; i <= 1000; i++) {
int die = (int) (1 + 6 * Math.random());
pw.print(die);
pw.print(" ");
if (i % 20 == 0)
pw.println();
}
pw.println();
pw.close(); // Without this, the output file may be empty
}
}
PrintWriter: println()
/*
* Output:
true
A
500
40000
45.67
45.67
Hello
99
*/
import java.io.PrintWriter;
public class MainClass {
public static void main(String args[]) {
try {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
PrintWriter: println(char x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
PrintWriter: println(double x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
PrintWriter: println(float x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
PrintWriter: println(int x)
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
try {
PrintWriter pw = new PrintWriter("c:\\temp\\printWriterOutput.txt");
pw.println("PrintWriter is easy to use.");
pw.println(1234);
pw.close();
} catch (IOException e) {
}
}
}
PrintWriter: println(long x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
PrintWriter: println(Object x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
PrintWriter: println(String x)
import java.io.PrintWriter;
public class Main {
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
}
}
Writer: write(char[] cbuf)
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) {
try {
char[] chars = new char[2];
chars[0] = "\u4F60";
chars[1] = "\u597D";
String encoding = "GB18030";
File textFile = new File("C:\\temp\\myFile.txt");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(textFile), encoding);
writer.write(chars);
writer.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}