FileWriter FileReader
public class test {
public static void main(String[] args){
File file = new File("D:/test.txt");
try {
FileWriter out = new FileWriter(file);
String buy = "你好";
out.write(buy);//参数必须为byte
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileReader in = new FileReader(file);
char buy[] = new char[10];
in.read(buy);//参数为char类型
System.out.println(new String(buy,0,buy.length));
in.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
BufferedWrite BufferedReader
public class test {
public static void main(String[] args){
File file = new File("D:/test.txt");
String[] txt = {"你好","世界"};
try {
FileWriter out = new FileWriter(file);
BufferedWriter bufw = new BufferedWriter(out);
for(int k = 0;k<txt.length;k++){
bufw.write(txt[k]);
bufw.newLine();
}
bufw.close();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileReader in = new FileReader(file);
BufferedReader bufr = new BufferedReader(in);
String s = null;
int i = 1;
while((s = bufr.readLine()) != null){
System.out.println("第" + i++ + "行 " + s);
}
bufr.close();
in.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
第1行 你好
第2行 世界
DataInputStream ....DataOutputStream
public class test {
public static void main(String[] args){
File file = new File("D:/test.txt");
try {
FileOutputStream out = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF("好的");
dos.writeBytes("你好");
dos.writeChars("世界");
dos.close();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);
String s = dis.readUTF();
System.out.println(s);
dis.close();
in.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}//read只能用utf来读取,因为他知道什么时候读取结束
//如果先write的不是UTF,那么也是无法成功读取的,因为不知道开始的位置
Zip压缩与解压暂时没看懂,,也不想看,,等着更新吧