Java IO

File.separator  (windows 表示 \     mac 表示 / )
@Value("${file.url}")
private String fileUrl;
//file.url=/Users/dongzejun/Desktop/ (application.properties的配置文件)
//创建文件 exists():判断是否存在,可能不存在
/*File file = new File(fileUrl + "aa.txt");
if (file != null && !file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(File.separator);
System.out.println(File.pathSeparator);
System.out.println("文件已存在!");
}*/
//删除文件
/*String fileName = "/Users/dongzejun/Desktop"+File.separator+"aa.txt";
File file =new File(fileName);
if(file.exists()){
file.delete();
}else {
System.out.println("文件不存在!");
}*/

/*创建文件夹 mkdirs在创建时创建全路径的文件夹 mkdir只创建最后一层的文件夹
isFile():判断是否文件,也许可能是文件或者目录*/
/* String fileName = "/Users/dongzejun/Desktop" + File.separator + "aa";
File f = new File(fileName);
if (!f.isFile()) {
f.mkdirs();
}*/

//列出某文件夹下面的所有文件
/*String filenName = "/Users/dongzejun/Desktop/aa" + File.separator;
File f = new File(filenName);
String[] str = f.list();
for (int i = 0, j = str.length; i < j; i++) {
System.out.println(str[i]);
}*/

//isDirectory判断某路径是否是目录
/* File f = new File(fileUrl);
if (!StringUtils.isEmpty(f) && f.isDirectory()) {
System.out.println("是文件目录");
} else {
System.out.println("不是文件目录!");
}*/

//创建新文件并用FileOutputStream字节流写入文字 (new FileOutputStream(f,true)设置true可追加文字!)
/*File f = new File(fileUrl + "test.txt");
OutputStream outputStream = null;
if (f != null) {
try {
if (!f.exists()) {
f.createNewFile();
}
outputStream = new FileOutputStream(f,true);
String str = "just say say!!!";
byte[] bytes = str.getBytes();
//outputStream.write(bytes);
for (int i = 0, j = bytes.length(); i < j; i++) {
outputStream.write(bytes[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}*/

//利用FileInputStream字节流读取文件 当读到文件末尾的时候会返回-1
/* File f = new File(fileUrl + "test.txt");
InputStream inputStream = null;
try {
if (f != null) {
inputStream = new FileInputStream(f);
byte[] bytes = new byte[(int) f.length()];
//inputStream.read(bytes);
int i = 0;
int j = 0;
while ((j = inputStream.read()) != (-1)) {
bytes[i++] = (byte) j;
}
//console输出读取的文件内容
System.out.println(new String(bytes).trim());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}*/
posted @ 2018-11-23 09:45  依旧爱你  阅读(167)  评论(0编辑  收藏  举报