温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中怎么实现输入输出流

发布时间:2021-06-11 14:48:46 来源:亿速云 阅读:151 作者:Leah 栏目:编程语言

Java中怎么实现输入输出流?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

字节输出流,输出到文件中(写)

OutputStream抽象类

选好文件

复制代码 代码如下:

File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt");

子类实例化,需要抛异常

稍后传输的数据覆盖原内容

OutputStream output = new FileOutputStream(file);

稍后传输的数据追加在原数据之后

OutputStream output = new FileOutputStream(file,true);

选好数据,转换为字节数组

String msg = "你好\r\n世界";
byte data[] = msg.getBytes();
  • windows系统txt文件换行用\r\n

输出到文件(向文件中写数据)

output.write(data);

写入部分数据

output.write(data,3,8);
  • 从字节数组data的脚标为3开始8个字节写入此输出流。

关闭输出流

output.close();

字节输入流,文件中数据读取到程序中(读)

InputStream抽象类

选好文件

File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt");

子类实例化,需要抛异常

FileInputStream input = new FileInputStream(file);

读取数据,并将数据保存到指定字节数组中

byte data[] = new byte[100];
int len = input.read(data);
  • 字节数组的长度需要选的合适,如果从文件中读取的内容的的字节数超过了指定字节数组的长度,那么超过的部分将无法保存到指定的字节数组中,这样内容就会丢失,如果遇到正文这样一个字占多个字节的,也可能出现乱码。

三种读取数据方式

byte data[] = new byte[100];
int len = input.read(data);

从此输入流中将最多 data.length 个字节的数据读入data数组中。在某些输入可用之前,此方法将阻塞。

返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。

byte data[] = new byte[100];
int len = input.read(data,5,14);

从此输入流中将最多14个字节的数据读入data数组中,读入位置为data数组后偏移5个位置,即读入的开始位置是数组脚标为5的位置

返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。

byte data[] = new byte[100];
int i = 0;
int read = input.read();
while (read != -1){
  data[i++] = (byte)read;
  read = input.read();
}

从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。

返回:下一个数据字节;如果已到达文件末尾,则返回 -1。

注意有参数的和无参数的read方法的返回值是不一样的,有参数的返回的是读取字节的个数,无参的返回的是字节的int值,如果要保存到字节数组,还需要强转为byte型。

将字节数组转换为字符串输出到控制台

String x = new String(data);
System.out.println(x);

关闭输入流

input.close();

字符输出流:Writer

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class Hello {
  public static void main(String[] args) throws Exception{
   File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt");
   Writer writer = new FileWriter(file);
   writer.write("你好\r\nworld");
   writer.close();
  }
}

write方法的参数直接写字符串即可。

必须要关闭字符输出流,不然数据只是保存在缓冲区中,并没有写入文件。

字符输入流:Reader

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
public class Hello {
  public static void main(String[] args) throws Exception {
   File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt");
   char data[] = new char[100];
   Reader reader = new FileReader(file);
   reader.read(data);
   String x = new String(data);
   System.out.println(x);
   reader.close();
  }
}

read方法是将读取的数据保存到字符数组中。

关于Java中怎么实现输入输出流问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI