io笔记

 import  java.io 

.File;
import  java.io 

.FileInputStream;
import  java.io 

.FileNotFoundException;
import  java.io 

.FileOutputStream;
import  java.io 

.FileReader;
import  java.io 

.FileWriter;
import  java.io 

.IOException;
import  java.io 

.UnsupportedEncodingException;

public class IODemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
//File的使用
File file = new File ("E:/a.txt");
System.out.println(file.length());
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.getParent());

File aa = new File("E:/aa");
if(aa.mkdir()){
System.out.println("aa创建成功....");
}
FileInputStream fis = null;
try{
fis = new FileInputStream("E:/a.txt");
byte[] buff = new byte[(int)file.length()];
int readbyte = fis.read(buff);
for (int i = 0; i < buff.length; i++) {
System.out.println(buff[i]);
}
String str = new String(buff);
System.out.println(str);

}
catch (FileNotFoundException e){

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fis = null;
}

String s1 = "这是输出的数据 1234567890qwertyuiopasdfghjklzxcvbnm";
byte[] sbuff = null;
FileOutputStream fos = null;
try {
sbuff = s1.getBytes("utf8");
fos =  new FileOutputStream("E:/at.txt");
fos.write(sbuff);
fos.flush();

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fos = null;
}

FileReader fr = null;

try {
fr = new FileReader("E:/a.txt");
char ch[] = new char[100];
int len = fr.read(ch);
for (int i = 0; i < len; i++) {
System.out.println(ch[i]);
}

String ss = new String(ch,0,len);
System.out.println(ss);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fr = null;
}

FileWriter fw = null;

try {
fw = new FileWriter("E:/at2.txt");
fw.write(s1);
fw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fw = null;
}


}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值