Java编程之文件读写实例详解

作者:Sunnyfans 时间:2022-01-27 11:13:40 

本文实例讲述了Java编程中文件读写的方法。分享给大家供大家参考,具体如下:

Java中文件读写操作的作用是什么?

回答这个问题时应该先想到的是Java只是一门语言,我们的一种使用工具而已,这样答案就明晰了,就是将外来的各种数据写入到某一个文件中去,用以保存下来;或者从文件中将其数据读取出来,供我们使用。就如下电影过程,从网络资源中下载一部电影保存于你电脑中(写文件),当你想看的时候就用播放器打开(读文件)。

Java中如何对文件进行读写操作?

先理一理,Java中的流分两种,字节流和字符流,其中字节流的两个基类是InputStream和OutputStream;字符流的两个基类是Reader和Writer。所谓文件流,即我们对文件的操作留不开流。由此可知我们要用到某个类必然继承如上的四个基类之一。Java中一切都是类,一切都是对象。自然会想到文件操作有哪些类:

如下四个直接用到的类:

字节流中:FileInputStream和FileOutputStream
字符流中:FileReader和FileWriter

找到类就好办事了。剩下来的就是去找实现方法啦。

两种选择方案在这里,这就牵涉到我们如何选择合适的文件读写方式呢?

选择条件的区别:

以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
以字符为单位读取文件,常用于读文本,数字等类型的文件.
至于是否选择用Buffer来对文件输入输出流进行封装,就要看文件的大小,若是大文件的读写,则选择Buffer这个桶来提供文件读写效率。

如下是简单运用实例:

1、运用字节流对文件进行直接读写:

注:FileOutputStream(file, true);里面true参数表示不覆盖原文件,直接在文件后面追加添加内容。


public class FileTest
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileOutputStream out = new FileOutputStream(file, true);
String s = "Hello,world!\r\n";
out.write(s.getBytes());
out.flush();
out.close();
//FileInputStream in = new FileInputStream(file);
//byte [] b = new byte[20];
//in.read(b, 0, b.length);
//System.out.println(new String(b));
//in.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

2、运用字符流对文件进行直接读写:


public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter(file,true);
fw.write("Hello,world!\r\n");
fw.flush();
fw.close();
//FileReader fr = new FileReader(file);
//int i=0;
//String s ="";
//while( ( i = fr.read() )!= -1)
//{
// s = s +(char)i;
//}
//System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

文件读写流用Buffer封装之后的运用:

1、对字节流封装后对文件进行读写:


static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileOutputStream out = new FileOutputStream(file, true);
// BufferedOutputStream bout = new BufferedOutputStream(out);
// String s = "I have a dream!";
// bout.write(s.getBytes());
// bout.flush();
// bout.close();
FileInputStream in = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(in);
byte[] b = new byte[15];
bin.read(b);
bin.close();
System.out.println(new String(b));
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

2、对字符流封装后对文件进行读写:


public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileWriter fw = new FileWriter(file, true);
// BufferedWriter bw = new BufferedWriter(fw);
// String nextLine = System.getProperty("line.separator");
// bw.write("Hello,world!" + nextLine);
// bw.flush();
// bw.close();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int i = 0;
String s = "";
String temp = null;
while((temp=br.readLine())!=null)
{
s = s+temp;
}
System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

希望本文所述对大家Java程序设计有所帮助。

标签:Java,文件读写
0
投稿

猜你喜欢

  • Java遍历Map对象的四种方式

    2022-05-07 14:30:04
  • spring cloud 配置中心客户端启动遇到的问题

    2023-03-23 16:30:34
  • C# goto语句的具体使用

    2021-07-22 22:26:22
  • SpringBoot 3.0 新特性内置声明式HTTP客户端实例详解

    2022-11-25 13:47:20
  • Java核心编程之文件随机读写类RandomAccessFile详解

    2023-11-28 17:40:05
  • Android仿新浪微博分页管理界面(3)

    2023-08-04 19:14:02
  • Java listener简介_动力节点Java学院整理

    2022-12-29 10:02:48
  • SpringSecurity报错authenticationManager must be spec的解决

    2021-07-27 21:52:27
  • Java集合遍历实现方法及泛型通配

    2022-02-26 13:55:54
  • Java String对象使用方法详解

    2023-12-14 14:43:52
  • java模拟TCP通信实现客户端上传文件到服务器端

    2023-11-26 10:14:49
  • Spring cloud alibaba之Gateway网关功能特征详解

    2022-10-26 18:22:28
  • 基于Java检查IPv6地址的合法性

    2022-08-05 12:17:17
  • C#如何用ThoughtWorks生成二维码

    2022-09-28 20:44:53
  • 浅析Java中comparator接口与Comparable接口的区别

    2023-11-01 20:31:14
  • java Springboot实现教务管理系统

    2023-01-18 00:28:12
  • Eureka源码阅读之环境搭建及工程结构

    2023-07-26 01:02:36
  • 详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类

    2021-08-04 03:16:28
  • 使用Spring Cache设置缓存条件操作

    2023-01-25 16:38:06
  • SpringBoot集成Redis流程详解

    2022-11-08 21:38:11
  • asp之家 软件编程 m.aspxhome.com