读字节流文件

本文提供了一个使用Java进行字节流文件读取的示例代码,通过FileInputStream和DataInputStream类来读取文件,并利用FileOutputStream将内容写入到另一个文件中。示例展示了如何打开文件、读取内容、写入新文件及关闭流的过程。

//读字节流文件

import java.io.*;
public class ReadFile
{
 public static void main(String []args)throws IOException
 {
  //File f= new File("D:\\J2EE\\mystudy\\src\\application.cfg.xml");
  //StringBuffer bu = new StringBuffer("");
  //File f = new File("C:\\Documents and Settings\\Administrator\\My Documents\\黄涛资料\\JT01班照片\\dsc_0036.jpg");
  //FileOutputStream fos = new FileOutputStream("huangtao.jpg");
  //File f = new File("D:/J2EE/mystudy/tom.txt");
  //File f = new File("C:/Documents and Settings/Administrator/����/bank.sql");
  //File f= new File("D:\\oracle\\bin\\oemapp.bat");
  //File f= new File("D:\\JCreator Pro\\JCreator_cn.exe");
  //File f= new File("e:\\bak\\","photoshop7.0����������ʽ��.rar");
  File f= new File("C:\\Documents and Settings\\Administrator\\����\\j2eesunweb_dg");
  //for(int j=0;j<=9;j++)
  //{
   FileOutputStream fos = new FileOutputStream("huangtao.txt");
   try
   {
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(new FileInputStream(f));
    DataOutputStream dos = new DataOutputStream(fos);
    byte b[]= new byte[1024];
    int i;
    while((i=fis.read(b, 0, 1024))!=-1)
    {
     fos.write(b, 0, i);
     fos.flush();
 //    fos.write(b, 0, i);
 //    fos.flush();
     //String a= new String(b,0,i);
     //System.out.println(bu.append(a));
    }
    dos.close();
    fos.close();
    dis.close();
    fis.close();
   }
   catch(IOException e)
   {
    e.printStackTrace();
   }
  //}
 }
}

### Java字节流取文本文件 在Java中,可以通过`FileInputStream`实现基于字节流的方式取文本文件的内容。以下是详细的说明以及代码示例。 #### 方法描述 使用`FileInputStream`可以逐字节地文件内容。如果需要处理字符数据,则需借助`InputStreamReader`将其转换为字符流,并可进一步设置编码方式以避免乱码问题[^2]。此外,为了提高性能,通常会结合`BufferedReader`或`BufferedInputStream`来提供缓冲功能[^4]。 #### 示例代码 以下是一个完整的代码示例,展示如何利用字节流取TXT文件: ```java import java.io.FileInputStream; import java.io.IOException; public class ByteStreamReadExample { public static void main(String[] args) { String filePath = "example.txt"; // 文件路径 StringBuilder contentBuilder = new StringBuilder(); try (FileInputStream fileInputStream = new FileInputStream(filePath)) { int byteData; while ((byteData = fileInputStream.read()) != -1) { contentBuilder.append((char) byteData); } } catch (IOException e) { System.err.println("Error reading the file: " + e.getMessage()); } System.out.println(contentBuilder.toString()); // 输出文件内容 } } ``` 此代码片段展示了基本的字节流取逻辑:通过循环调用`read()`方法逐一获取字节直到返回值为-1表示结束[^1]。随后将每个字节转化为对应的字符并拼接到字符串构建器中。 对于包含特殊编码(如UTF-8)的文件,推荐如下改进版本: ```java import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; public class Utf8ByteStreamReadExample { public static void main(String[] args) { String filePath = "utf8_example.txt"; StringBuilder contentBuilder = new StringBuilder(); try (FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) { int charData; while ((charData = isr.read()) != -1) { contentBuilder.append((char) charData); } } catch (IOException e) { System.err.println("Error occurred during file processing: " + e.getMessage()); } System.out.println(contentBuilder.toString()); } } ``` 在此增强版中引入了`InputStreamReader`用于指定具体的字符集名称(这里是UTF-8),从而有效防止因默认平台编码而导致的中文或其他多字节字符显示异常的情况发生[^2]。 #### 性能优化建议 当面对大容量的数据传输场景时,单纯依赖单次取一个字节效率较低,因此实际开发过程中更倾向于批量加载一定数量级的数据块进入内存后再做后续解析工作。例如采用数组形式一次性接受多个连续存储单元内的信息量。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值