io流 字节文件流FileOutputStream与FileInputStream实现
---恢复内容开始---
import java.io.*;
/**
* Created by lxj-pc on 2017/6/25.
*/
public class WriterJavaApp {
public static void main(String[] args) {
String fileName="Hello.java";
String dirPath="d:/demo/day01";
//java.io.File
File dir=new File(dirPath);
if(!dir.exists())
dir.mkdirs();
//字节文件流:FileOutputStream
//字符文件流:FileWriter
try {
FileOutputStream fos = new FileOutputStream(new File(dir,fileName));
fos.write("public class Hello{\r\n".getBytes());
fos.write("public static void main(String[] args) {\r\n".getBytes());
fos.write("System.out.prinntln(\"你好\");\r\n".getBytes());
fos.write("}".getBytes());
fos.write("}".getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("写入成功!");
readTxt(dirPath+"/"+fileName);
}
//读取指定绝对路径文件的文本内容 filePath
public static void readTxt(String filePath) {
//文件字符流 FileReader
//文件字节流 FIS
File file =new File(filePath);
if(!file.exists()){
System.out.println(filePath+"不存在文件");
}
try {
FileInputStream fis= null;
fis = new FileInputStream(file);
byte[] bytes= new byte[0];
try {
bytes = new byte[fis.available()];
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("读完");
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
---恢复内容结束---