import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest01 {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("myfile");
char[] chars = new char[4];
int readCount = 0;
fr.read(chars);
for(char data : chars){
System.out.println(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest01 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("myfile02",true);
char[] chars = {
'我','是','中','国','人'};
fw.write(chars);
fw.write(chars,2,3);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DataInputStreamTest01 {
public static void main(String[] args) {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("jiamitext01"));
byte b = dis.readByte();
short s = dis.readShort();
int i = dis.readInt();
long l = dis.readLong();
float f = dis.readFloat();
double d = dis.readDouble();
boolean boo = dis.readBoolean();
char c = dis.readChar();
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(boo);
System.out.println(c);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
public class DataOutputStreamTest01 {
public static void main(String[] args) {
DataOutputStream dop = null;
try {
dop = new DataOutputStream(new FileOutputStream("jiamitext01"));
byte b = 122;
short s = 233;
int i = 345;
long l = 567;
float f = 12.34f;
double d = 234.234;
boolean boo = true;
char c = 'a';
dop.writeByte(b);
dop.writeShort(s);
dop.writeInt(i);
dop.writeLong(l);
dop.writeFloat(f);
dop.writeDouble(d);
dop.writeBoolean(boo);
dop.writeChar(c);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dop != null) {
try {
dop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}