package fileStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String projectRootPath = new File("").getAbsolutePath();//获取项目路径
// String projectRootParentPath = new File("..").getCanonicalPath();
// File fileFolder = new File(projectRootParentPath+File.separator+"files");//规范路径名(将.以及..进行转换)
// if(!fileFolder.exists()){
// fileFolder.mkdir();
// }
// File file = new File(fileFolder.getCanonicalFile()+File.separator+"hello.txt");
File file = new File(projectRootPath+File.separator+"files"+File.separator+"hello.txt");
if(!file.exists()){
file.createNewFile();
}
//将字符串写入文件
String str = "hello world,my name is bing!";
byte[] bytes = str.getBytes();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();
//从文本文件中读取内容
FileInputStream fis = new FileInputStream(file);
byte[] bytes2 = new byte[(int)file.length()];
fis.read(bytes2);
fis.close();
System.err.println(file.getCanonicalPath()+"\n"+new String(bytes2));
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String projectRootPath = new File("").getAbsolutePath();//获取项目路径
// String projectRootParentPath = new File("..").getCanonicalPath();
// File fileFolder = new File(projectRootParentPath+File.separator+"files");//规范路径名(将.以及..进行转换)
// if(!fileFolder.exists()){
// fileFolder.mkdir();
// }
// File file = new File(fileFolder.getCanonicalFile()+File.separator+"hello.txt");
File file = new File(projectRootPath+File.separator+"files"+File.separator+"hello.txt");
if(!file.exists()){
file.createNewFile();
}
//将字符串写入文件
String str = "hello world,my name is bing!";
byte[] bytes = str.getBytes();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();
//从文本文件中读取内容
FileInputStream fis = new FileInputStream(file);
byte[] bytes2 = new byte[(int)file.length()];
fis.read(bytes2);
fis.close();
System.err.println(file.getCanonicalPath()+"\n"+new String(bytes2));
}
}