getAbsolutePath、getCanonicalPath、FileInputStream、FileOutputStream
来源:互联网 发布:dotamax数据更新 编辑:程序博客网 时间:2024/05/18 05:53
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));
}
}
0 0
- getAbsolutePath、getCanonicalPath、FileInputStream、FileOutputStream
- File getAbsolutePath GetCanonicalPath
- File:getPath()&getAbsolutePath()&getCanonicalPath()
- getPath(),getAbsolutePath(),getCanonicalPath()区别
- getpath() & getAbsolutePath() & getCanonicalPath()
- FileInputStream&&FileOutputStream
- FileOutputStream/FileInputStream
- FileInputStream/FileOutputStream
- FileOutputStream&FileInputStream
- FileInputStream FileOutputStream
- FileInputStream||FileOutputStream
- Java getPath() getAbsolutePath() getCanonicalPath()区别
- file.getPath() getAbsolutePath() getCanonicalPath()区别
- file.getPath() getAbsolutePath() getCanonicalPath()区别
- file.getPath() getAbsolutePath() getCanonicalPath()区别
- getPath getAbsolutePath和getCanonicalPath的不同
- getPath getAbsolutePath和getCanonicalPath的区别
- getPath getAbsolutePath和getCanonicalPath的区别
- 三分算法学习
- Java 之 看透她的心 《Java文档》
- 射频宽带放大器-国电
- [Android] 调试native代码时查看错误stack
- Linux sed 使用
- getAbsolutePath、getCanonicalPath、FileInputStream、FileOutputStream
- Linux ssh 免密码登入远程服务器
- ACM POJ 2593 Max Sequence ----最大子段和问题
- 福晶科技当空参加过花电话费吃喝是滴
- Codeforces Round #267 (Div. 2) 解题报告
- BA已跪
- Codeforces Round #267 (Div. 2)467B 467C
- Java 之 何以一见钟情 《第一个Java程序和Applet程序》
- 设计模式——工厂模式(Factory Pattern)