本篇文章为大家展示了如何在Java中创建和读写File文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1.创建一个文件
@Test
public void test6() throws IOException {
File file1 = new File("C:\\IDEA\\h2.txt");
if(!file1.exists()){//文件不存在
file1.createNewFile();
System.out.println("创建成功");
}else{//文件存在
file1.delete();
System.out.println("删除成功");
}
}
输出
file.mkdir,不会帮你创建上层目录 file.mkdirs,会帮你创建上层目录
@Test
public void test7(){
//创建文件夹,mkdir,不会帮你创建上层目录
File file1 = new File("c:\\IDEA\\io2");
boolean mkdir =file1.mkdir();
if(mkdir){
System.out.println("创建成功1");
}
//创建文件夹,mkdirs,会帮你创建上层目录
File file2 = new File("c:\\IDEA\\io1\\io3");
boolean mkdirs =file2.mkdirs();
if(mkdirs){
System.out.println("创建成功2");
}
}
输出
@Test
public void test8(){
//删除文件或空文件夹
File file1 = new File("c:\\IDEA\\h2.txt");
file1.delete();
}
//递归函数删除所有文件
private boolean deletedir(File dir){
if (dir.isDirectory()) {
File[] files = dir.listFiles();
//递归删除目录中的子目录下
for (File f:files) {
boolean success = deletedir(f);
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
@Test
public void test8() {
File dir = new File("c:\\IDEA");
System.out.println(deletedir(dir));
}
1.对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2.对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt)使用字节流处理
@Test
public void test9() {
FileReader fr = null;//自动补的
try {
//1.实例化File类的对象,指明要操作的文件
File file1 = new File("c:\\IDEA\\hello.txt");
file1.createNewFile();//要抛出异常
//2.提供具体的流
fr = new FileReader(file1);
//3.数据的读入
//read():返回读入的一个字符,如果达到文件末尾,返回-1
int data = fr.read();
while(data!=-1){
System.out.print((char)data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4.流的关闭操作
if(fr!=null)//防止没有实例化成功,避免空指针异常
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
要记得关闭,因为物理连接JVM垃圾回收机制不会自动回收,要手动关闭。
@Test
public void test1() {
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
//错误的写法
//for(int i=0;i<cbuf.length;i++{
// System.out.println(cbuf[i]);
//}
//正确的写法
for (int i = 0; i < len; i++) {
System.out.println(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
try {
//4.资源的关闭
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test2() throws IOException{
//File对应的硬盘中的文件
// 如果不存在,在输出的过程中,会自动创建此文件
//1.提供File类的对象,指明写出到的文件
FileWriter fw = null;
try {
File file = new File("hello.txt");
//2.提供FileWriter的对象,用于数据的写出
//FileWriter(file,append)第二个参数,append是true则在后面添加,是false就覆盖
fw = new FileWriter(file,true);
//3.写出的操作
fw.write("I have a dream!");
fw.write("you need have a dream");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw!=null)
//4.流资源的关闭
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test3(){
FileReader fr = null;
FileWriter fw = null;
try {
// 1.创建File类的对象,指明读入和写出的文件
File src = new File("hello.txt");
File des = new File("hello1.txt");
// 2.创建输入输出流的对象
fr = new FileReader(src);
fw = new FileWriter(des,true);//不覆盖
// 3.数据的读入和写出操作
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf))!=-1){
//每次写出len个字符
fw.write(cbuf,0,len);//从cbuf的0号位开始写入len个字符
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 4.关闭流资源1
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 4.关闭流资源2
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test4(){
FileInputStream fis=null;
FileOutputStream fos=null;
try {
//1.造文件
File src = new File("b.jpg");
File des = new File("c.jpg");
//2.造流
fis = new FileInputStream(src);
fos = new FileOutputStream(des);
//3.读数据,存数据
byte[] buffer = new byte[5];
int len;//记录每次读取的字节的个数
while((len = fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null) {
try {
//4.关闭资源
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
//4.关闭资源
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test5(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File src = new File("b.jpg");
File des = new File("d.jpg");
//2.造流
//2.1造节点流
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(des);
//2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.复制的细节:读取,写入
byte[] buffer =new byte[10];
int len;
while((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源关闭
//要求,先关闭外层的流,再关闭内层的流
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//说明:关闭外层流的同时,内层自动关闭,所以外层关闭可以省略
//fos.close();
//fis.close();
}
}
用缓冲流快了很多
1. 简单,只需理解基本的概念,就可以编写适合于各种情况的应用程序;2. 面向对象;3. 分布性,Java是面向网络的语言;4. 鲁棒性,java提供自动垃圾收集来进行内存管理,防止程序员在管理内存时容易产生的错误。;5. 安全性,用于网络、分布环境下的Java必须防止病毒的入侵。6. 体系结构中立,只要安装了Java运行时系统,就可在任意处理器上运行。7. 可移植性,Java可以方便地移植到网络上的不同机器。8.解释执行,Java解释器直接对Java字节码进行解释执行。
上述内容就是如何在Java中创建和读写File文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。