IO
InputStream
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
InputStream is = new FileInputStream(file);
int bit = is.read();
System.out.println((char)bit);
bit = is.read();
System.out.println((char)bit);
}
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
InputStream is = new FileInputStream(file);
int num = 0;
while((num=is.read())!=-1) {
System.out.println((char)num);
}
is.close();
}
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
InputStream is = new FileInputStream(file);
byte[] b = new byte[1024];
int len = is.read(b);
String msg = new String(b,0,len);
System.out.println(msg.toString());
is.close();
}
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("a.txt");
ObjectInputStream ois = new ObjectInputStream(is);
int num = ois.readInt();
System.out.println(num);
double d = ois.readDouble();
System.out.println(d);
String s =ois.readUTF();
System.out.println(s);
}
OutputStream
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt");
int num = 97;
os.write(num);
os.close();
}
public class Test5 {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt",true);
String msg = "Happeniness";
os.write(msg.getBytes(), 0, msg.length());
os.close();
}
public class Test {
public static void main(String[] args) throws IOException {
OutputStream f = new FileOutputStream("a.txt");
ObjectOutputStream os = new ObjectOutputStream(f);
os.writeInt(123);
os.writeDouble(10.2);
os.writeUTF("Unhappiness");
os.close();
}
Reader
public class Test1 {
public static void main(String[] args) throws IOException {
InputStream f = new FileInputStream("a.txt");
Reader reader = new InputStreamReader(f);
int num = reader.read();
System.out.println(num);
reader.close();
}
public static void main(String[] args) throws IOException {
InputStream f = new FileInputStream("a.txt");
Reader reader = new InputStreamReader(f);
int num =0;
while((num=reader.read())!=-1) {
System.out.println((char)num);
}
reader.close();
}
public static void main(String[] args) throws IOException {
InputStream f = new FileInputStream("a.txt");
Reader reader = new InputStreamReader(f);
char[] chs = new char[1024];
int count = reader.read(chs);
System.out.println(new String(chs,0,count));
reader.close();
}
public class Test {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt");
Writer out = new OutputStreamWriter(os);
char ch = '帅';
out.write(ch);
out.flush();
out.close();
}
}
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt");
Writer out = new OutputStreamWriter(os);
String str = "你好";
out.write(str);
out.flush();
out.close();
}
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt");
Writer out = new OutputStreamWriter(os);
String str = "whata Fuck";
out.write(str.toCharArray());
out.close();
}
BufferedReader
public class Test06 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("a.txt")));
String str = null;
StringBuilder sb = new StringBuilder();
while((str=reader.readLine())!=null) {
sb.append(str);
sb.append("\r\n");
}
System.out.println(sb.toString());
reader.close();
}
}
BufferedWriter
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt");
Writer out = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(out);
bw.write("我自横刀向天笑");
bw.newLine();
bw.write("去留肝胆两昆仑");
bw.flush();
bw.close();
out.close();
os.close();
}