右点击项目 new- untitled-<在里面输入东西,保存到你项目中>
java:
public class ReadByteStream {
public static void main(String[] args) {
try { //读取文件的字节流
FileInputStream fis = new FileInputStream("text.txt");//此处放的是相对位置(这样是根目录中)
byte input[] = new byte[20];//字节不能大于文件中的字节
fis.read(input);
String inputString = new String(input,"gbk");//默认是utf-8
System.out.println("打印:"+inputString);
} catch (Exception e) {
// TODO: handle exception
}
}
}
文件的写入:
public class WriteByteStream {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("text.txt");
String outString = "write 123456写出数据";
byte output[] = outString.getBytes("gbk");
fos.write(output);
fos.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
文件的拷贝:
public class CopyByByteStream {
public static void main(String[] args) {
try {//拷贝
FileInputStream fis = new FileInputStream("tt.jpg");
FileOutputStream fos = new FileOutputStream("aa.jpg");
byte input[] = new byte[10];
while (fis.read(input)!=-1){//读取文件,等于-1是说明文件读取已经到了末尾
fos.write(input);//写入文件
System.out.println("1111111");
}
fis.close();
fos.close();
System.out.println("done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}