Java中IO整理完整版(4)

2025-07-22

? ? ? ? ?

import java.io.*; class hello{

public static void main(String[] args) throws IOException { String fileName=\ File f=new File(fileName);

?? Writer out =new FileWriter(f); ?? String str=\?? out.write(str); ?? out.close(); ?? } ?? }

当你打开hello。txt的时候,会看到hello

其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。

当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

Writer out =new FileWriter(f,true);

这样,当你运行程序的时候,会发现文件内容变为: hellohello如果想在文件中换行的话,需要使用“\\r\\n”

比如将str变为String str=\

这样文件追加的str的内容就会换行了。

从文件中读内容:

?? /** ?? * 字符流

?? * 从文件中读出内容 ?? * */

?? import java.io.*; ?? class hello{

?? public static void main(String[] args) throws IOException { ?? String fileName=\ ?? File f=new File(fileName); ?? char[] ch=new char[100]; ?? Reader read=new FileReader(f);

?? int count=read.read(ch); ?? read.close();

?? System.out.println(\读入的长度为:\

?? System.out.println(\内容为\new String(ch,0,count)); ?? } ?? }

【运行结果】: 读入的长度为:17

内容为hellohello

hello

当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。

?? /** ?? * 字符流

?? * 从文件中读出内容 ?? * */

?? import java.io.*; ?? class hello{

?? public static void main(String[] args) throws IOException { ?? String fileName=\ ?? File f=new File(fileName); ?? char[] ch=new char[100]; ?? Reader read=new FileReader(f); ?? int temp=0; ?? int count=0;

?? while((temp=read.read())!=(-1)){ ?? ch[count++]=(char)temp; ?? }

?? read.close();

?? System.out.println(\内容为\new String(ch,0,count)); ?? } ?? }

运行结果: 内容为hellohello

hello

关于字节流和字符流的区别

实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。

读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。

使用字节流好还是字符流好呢?

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

文件的复制

其实DOS下就有一个文件复制功能,比如我们想把d盘下面的hello.txt文件复制到d盘下面的rollen.txt文件中,那么我们就可以使用下面的命令:

copy d:\\hello.txt d:\\rollen.txt

运行之后你会在d盘中看见hello.txt.,并且两个文件的内容是一样的,(这是屁话)

下面我们使用程序来复制文件吧。

基本思路还是从一个文件中读入内容,边读边写入另一个文件,就是这么简单。、 首先编写下面的代码:

?? /**

?? * 文件的复制 ?? * */

?? import java.io.*; ?? class hello{

?? public static void main(String[] args) throws IOException { ?? if(args.length!=2){

?? System.out.println(\命令行参数输入有误,请检查\ ?? System.exit(1);

?? }

?? File file1=new File(args[0]); ?? File file2=new File(args[1]); ??

?? if(!file1.exists()){

?? System.out.println(\被复制的文件不存在\?? System.exit(1); ?? }

?? InputStream input=new FileInputStream(file1); ?? OutputStream output=new FileOutputStream(file2); ?? if((input!=null)&&(output!=null)){ ?? int temp=0;

?? while((temp=input.read())!=(-1)){ ?? output.write(temp); ?? } ?? }

?? input.close(); ?? output.close(); ?? } ?? }

然后在命令行下面 javac hello.java

java hello d:\\hello.txt d:\\rollen.txt

现在你就会在d盘看到rollen。txt了,

OutputStreramWriter 和InputStreamReader类

整个IO类中除了字节流和字符流还包括字节和字符转换流。

OutputStreramWriter将输出的字符流转化为字节流

InputStreamReader将输入的字节流转换为字符流

但是不管如何操作,最后都是以字节的形式保存在文件中的。

将字节输出流转化为字符输出流

?? /**

?? * 将字节输出流转化为字符输出流 ?? * */

?? import java.io.*; ?? class hello{

?? public static void main(String[] args) throws IOException { ?? String fileName= \?? File file=new File(fileName);

?? Writer out=new OutputStreamWriter(new FileOutputStream(file)); ?? out.write(\ ?? out.close(); ?? } ?? }

运行结果:文件中内容为:hello

将字节输入流变为字符输入流

?? /**

?? * 将字节输入流变为字符输入流 ?? * */

?? import java.io.*; ?? class hello{

??? public static void main(String[] args) throws IOException { ??? String fileName= \??? File file=new File(fileName);

??? Reader read=new InputStreamReader(new FileInputStream(file)); ??? char[] b=new char[100]; ??? int len=read.read(b);

??? System.out.println(new String(b,0,len)); ??? read.close(); ??? } ??? }

【运行结果】:hello

前面列举的输出输入都是以文件进行的,现在我们以内容为输出输入目的地,使用内存操作流

ByteArrayInputStream 主要将内容写入内容


Java中IO整理完整版(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:统计业务知识

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219