Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream)

本文主要是介绍Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://wolfgangkiefer.blog.163.com/blog/static/86265503201182795254987/


数据流可分节点流(想象成一管子直接接文件或内存等上),处理流(套在节点流之外使用).

一、理解数据流:
流一般分为输入流Input Stream和输出流Output Stream.
Java的标准数据流:
指在字符方式下,程序与系统进行交互的方式.标准输入studin,对象是键盘.标准输出stdout,对象是屏幕.标准错误输出stderr,对象是屏幕.

例:
本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,count保存实际读入的字节个数,再以整数和字符两种方式输出buffer中的值.
import java.io.*;
public class Input1
{
        public static void main(String args[]) throws IOException
        {
             System.out.println("Input: ");
             byte buffer[]=new byte[512];      //输入缓冲区
             int count=System.in.read(buffer);       //读取标准输入流
             System.out.println("Output: ");
             for (int i=0;i<count;i++)        //输出buffer元素值
             {
                 System.out.print(" "+buffer[i]);
             }
             System.out.println();
             for (int i=0;i<count;i++)      //按字符方式输出buffer
             {
                 System.out.print((char)buffer[i]);
             }
             System.out.println("count="+count);
        }
}
程序中,main方式采用throws子句抛出IOException异常交由系统处理.

字节流:
从InputStream和OutputStream派生出来的一系列类.这类流以字节byte为基本处理单位
          FileInputStream      FileOutputStream
          PipedInputStream      PipedOutputStream
          ByteArrayInputStream ByteArrayOutputStream
          FilterInputStream      FilterOutputStream
          DataInputStream      DataOutputStream
          BufferedInputStream BufferedOutputStream
字符流:
从Reader和Writer派生出来的一系列类,以16位的Unicode码表示的字符为基本处理单位
InputStreadmReader      OutputStreadmWriter
FileReader      FileWriter
CharArrayReader CharArrayWriter
PipedReader PipedWriter
FilterReader      FilterWriter
BufferedReader BufferedWriter
StringReader      StringWriter

字节流初步:
read() 从流中读入数据
skip() 跳过流中若干字节数
available() 返回流中可用字节数
mark() 在流中标记一个位置
reset() 返回标记过得位置
markSupport() 是否支持标记和复位操作
close()
write()      输出到流
flush() 刷空输出流

例:
本例以FileInpuStream的read(buffer)方法,每次从源程序文件OpenFile.java中读取512个字节,存储在缓冲区buffer中,再将以buffer中的值构造的字符串new String(buffer)显示在屏幕上
import java.io.*;
public class OpenFile
{
        public static void main(String args[]) throws IOException
        {
            try
            {
                 FileInputStream rf=new FileInputStream("OpenFIle.java");
                 int n=512;
                 byte buffer[]=new byte[n];
                 while      ((rf.read(buffer,0,n)!=1)&&(n>0))
                 {
                      System.out.print(new String(buffer));
                  }
                  System.out.println();
                  rf.close();
             }
             catch(IOException ioe)
             { System.out.println(ioe); }
             catch(Exception e)
             { System.out.println(e); }
        }
}
例:写入文件
本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,再以FileOutStream的writer(buffer)方法,将buffer中内容写入文件中.
import java.io.*;
public class Writer
{
       public static void main(String args[])
       {
          try
          {
              System.out.print("Input: ");
              int count,n=512;
              byte buffer[]=new byte[n];
              count=System.in.read(buffer);
              FileOuputStream wf=new FileOutputStream("Writer.txt");
              wf.write(buffer,0,count);
              wf.close();
          }
          catch(Exception e)
          { System.out.print(e); }
}

FileInputStream、FileOutputStream综合示例:

import java.io.*;

public class FileStream {
    public static void main(String[] args) throws Exception{
      FileOutputStream out=new FileOutputStream("Myhello.txt");
      out.write("Welcome to 香巴拉空间~".getBytes());
      out.close();
      byte [] buf=new byte[1024];
      File f=new File("Myhello.txt");
      FileInputStream in=new FileInputStream(f);
      int len=in.read(buf);
      System.out.println(new String(buf,0,len));
      in.close();
    }
}

字符流: Reader Writer

FileReader 、FileWriter应用示例:

mport java.io.*;

public class FileStream2 {
   public static void main(String[] args) throws Exception{
//     FileStream2 filestream2 = new FileStream2();
       FileWriter out=new FileWriter("File_tmp2.txt");
       out.write("欢迎来到香巴拉空间~ , FileWriter");
       out.close();
       char [] buf=new char[1024];
       FileReader in=new FileReader("File_tmp2.txt");
       int len=in.read(buf);
       System.out.println(new String(buf,0,len));      
    }
}

例: 文件编辑器
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class EditFile1 extends WindowAdapter implements ActionListener,TextListener
{
       Frame f;
       TextArea ta1;
       Panel p1;
       TextField tf1;
       Button b1,b2,b3;
       FileDialog fd;
       File file1=null;
public static void main(String args[])
{
(new EditFile1()).display();
}
public void display()
{
f = new Frame("EditFile");
f.setSize(680,400);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
tf1 = new TextField();
tf1.setEnabled(false);
tf1.setFont(new Font("Dialog",0,20)); //设置文本行的初始字体
f.add(tf1,"North");
ta1 = new TextArea();
ta1.setFont(new Font("Dialog",0,20)); //设置文本区的初始字体
f.add(ta1);
ta1.addTextListener(this); //注册文本区的事件监听程序
p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
b1 = new Button("Open");
b2 = new Button("Save");
b3 = new Button("Save As");
p1.add(b1);
p1.add(b2);
p1.add(b3);
b2.setEnabled(false);
b3.setEnabled(false);
b1.addActionListener(this); //注册按钮的事件监听程序
b2.addActionListener(this);
b3.addActionListener(this);
f.add(p1,"South");
f.setVisible(true);
}
public void textValueChanged(TextEvent e)
{ //实现TextListener接口中的方法,对文本区操作时触发
b2.setEnabled(true);
b3.setEnabled(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1) //单击[打开]按钮时
{
fd = new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(true); //创建并显示打开文件对话框
if ((fd.getDirectory()!=null) && (fd.getFile()!=null))
{
tf1.setText(fd.getDirectory()+fd.getFile());
try //以缓冲区方式读取文件内容
{
file1 = new File(fd.getDirectory(),fd.getFile());
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
String aline;
while ((aline=br.readLine()) != null)//按行读取文本
ta1.append(aline+"\r\n");
fr.close();
br.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
if ((e.getSource()==b2) || (e.getSource()==b3))
{ //单击[保存]按钮时
if ((e.getSource()==b3) ||(e.getSource()==b2)&&(file1==null))
{ //单击[SaveAs]按钮时,或单击[Save]按钮且文件对象为空时
fd = new FileDialog(f,"Save",FileDialog.SAVE);
if (file1==null)
fd.setFile("Edit1.txt");
else
fd.setFile(file1.getName());
fd.setVisible(true); //创建并显示保存文件对话框

if ((fd.getDirectory()!=null) && (fd.getFile()!=null))
{
tf1.setText(fd.getDirectory()+fd.getFile());
file1 = new File(fd.getDirectory(),fd.getFile());
save(file1);
}
}
else
save(file1);
}
}
public void save(File file1)
{
try //将文本区内容写入字符输出流
{
FileWriter fw = new FileWriter(file1);
fw.write(ta1.getText());
fw.close();
b2.setEnabled(false);
b3.setEnabled(false);
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

文件操作
File类:
File(String pathname)
File(File patent,String chile)
File(String patent,String child)
String getName()      得到文件名(不含路径)
String getPath() 得到文件路径
String getAbslutePath()      得到文件绝对路径
String getParent()      得到文件上一级目录名
String renameTo(File newName)       将当前文件更名为给定文件的完整路径
boolean exists() 文件是否存在
boolean canWrite()
boolean canRead()
boolean isFile() 是否是文件(而不是目录)
boolean isDirectory()
long lastModified()
long length()
boolean delete() 删除当前文件
boolean mkdir()
String list()       列出当前目录下的文件
例:自动更新文件
本例使用File类对象对指定文件进行自动更新的操作
import java.io.*;
import java.util.Data;
import java.text.SimpleDateFormat;
public class UpdateFile
{
       public static void main(String args[]) throws IOException
       {
           String fname="Write.txt";
           String childdir="backup"
           new UpdateFile().update(fname,childdir);
       }
       public void update(String fname,String childdir) throws IOException
       {
          File f1,f2,child;
          f1=new File(fname);
          child=new File(childdir)
          if (f1.exists())
             {
                if (!child.exists())child.mkdir();        //如果目录不存在则建立
                f2=new File(child,fname);
                if (!f2.exists() || f2.exists() && (f1.lastModified()>f2.lastModified()))      //如果f2不存在,或存在但日期较f1早
                copy(f1,f2)
                getinfo(f1);
                getinfo(child);
           }
           else
                 System.out.println(f1.getName()+" file not found!");
       }
       public void copy(File f1,FIle f2) throws IOException
       {
           FileInputStream rf=new FileInputStream(f1);
           FileOutputStreadm w=new FileOutputStream(f2);
           int count,n=512;
           byte buffer[]=new byte[n];
           count=rf.read(buffer,0,n);
           while(count!=-1)
            {
                wf.write(buffer,0,count)
                count=rf.read(buffer,0,n);
            }
            System.out.println("CopyFile "+f2.getName()+" !");
            rf.close();
            wf.close();
       }
       public static void getinfo(File f1)      throws IOException
       {
          SimpleDateFormat sdf;
          sdf=new SimpleDateFormat("yyyy年MM月dd日hh时mm分);
          if (f1.isFile())
              System.out.println("<FILE>\t"+f1.getAbsolutePath()+"\t"+f1.lenght()+"\t"+f1.lenght()+"\t"+sdf.format(new Date(f1.lastModified())));
          else
               {
                  System.out.println("\t"+f1.getAbsolutePath());
                  File[] files=f1.listFiles();
                  for (int i=0;i<FILES.Lenght;i++)
                       getInfo(files[i]);
               }
         }
}

文件过滤器
类FilterInputStream和FilterOutputStream分别对其他输入/输出流进行特殊处理,它们在读/写数据的同时可以对数据进行特殊处理.另外还提供了同步机制,使得某一时刻只有一个线程可以访问一个输入/输出流
要使用过滤流,首先必须把它连接到某个输入/输出流上: FilterInputStream(InputStream in);      FilterOutputStream(OutputStream out);
例: 列出当前目录中带过滤器的文件名清单
import java.io.*;
public class DirFilter implements FilenameFIlter
{
       private String prefix="",suffix=""       //文件名的前缀和后缀
       public DirFilter(String filterstr)
       {
          filterstr=filterstr.toLowerCase();
          int i=filterstr.indexOf('*');
          int j=filterstr.indexOf('.');
          if (i>0)
             prefix=filterstr.substring(0,i);
          if(j>0)
             suffix=filterstr.substring(j+1);
       }
       public static void main(String args[])
       {
          FilenameFIlter filter=new DirFilter("w*abc.txt");
          File f1=new File("");
          File curdir=new File(f1.getAbsolutePath(),"");
          System.out.println(curdir.getAbsolutePath();
          String[] str=curdir.list(filter);      //列出带过滤器的文件名清单
          for (int i=0;i<Str.length,i++)
               System.out.println("\t"+str[i]);
       }
       public boolean accept(File dir,String filename)
       {
          boolean yes=true;
          try
          {
              filename=filename.toLowerCase();
              yes=(filename.startsWith(prefix)) & (filename.endsWidth(suffix));
          }
          catch(NullPointerExceptin e)
          { }
          return yes;
       }
}
----------------------------------------------------------------------------------
自己修改了一点,带命令行参数的
import java.io.*;

public class DirFilter implements FilenameFilter
{
private String prefix="",suffix=""; //文件名的前缀、后缀
public DirFilter(String filterstr)
{
filterstr = filterstr.toLowerCase();
int i = filterstr.indexOf('*');
int j = filterstr.indexOf('.');
if (i>0)
prefix = filterstr.substring(0,i);
if (j>0)
suffix = filterstr.substring(j+1);
}
public static void main(String args[])
{ //创建带通配符的文件名过滤器对象
FilenameFilter filter;
if(args.length>0)
{filter = new DirFilter(args[0]);}
else
{filter= new DirFilter("p*.class");}
File f1 = new File("");
File curdir = new File(f1.getAbsolutePath(),""); //当前目录
System.out.println(curdir.getAbsolutePath());
String[] str = curdir.list(filter); //列出带过滤器的文件名清单
for (int i=0;i<str.length;i++)
System.out.println("\t"+str[i]);
}
public boolean accept(File dir, String filename)
{
boolean yes = true;
try
{
filename = filename.toLowerCase();
yes = (filename.startsWith(prefix)) &
(filename.endsWith(suffix));
}
catch(NullPointerException e)
{
}
return yes;
}
}
----------------------------------------------------------------------------------
对于InputStream和OutputStream来说,它们的实例都是顺序访问流.在java中,类RandomAccessFile提供了随机访问文件的方法
public class RandomAccessFile extends Object implements DataInput,DataOutput
类RandomAccessFile允许对文件内容同时进行读写: Datainput DataOutput
int skipBytes(int n) 指针向下移动若干字节
redInt. writeDouble ...
length() 返回文件长度
long getFilePointer() 指针当前位置
void seek(long pos) 指针调到所需位置
void setLenght(long newLenght) 设置文件长度
RandomAccessFile(File file,String mode)
RandomAccessFile(String name,String mode)
r      只读      rw 读写      rws 同步读写       rwd 数据同步读写

例:随机文件操作
import java.io.*;
public class PrimesFIle
{
       RandomAccessFile raf;
       public static void main(String args[]) throws IOException
       {
          (nw PrimesFile()).createprime(100);
       }
       public void createprime(int max) throws IOException
       {
           raf=new RandomAccessFile("primes.bin","rw");
           raf.seek(0);
           raf.writeInt(2);      //写入整形
           int k=3;
           while (k<=max)
           {
               if(isPrime(k))
                   raf.writeInt(k);
               k=k+2;
           }
           output(max);
           raf.close();
       }
       public boolean isPrime(int k) throws IOException
       {
         int i=0,j;
         boolean yes=true;
         try
         {
            raf.seek(0);
            int count=(int)(raf.lenght()/4);
            while ((i<=count) && yes)
            {
              if (k % raf.readInt()==0)
                  yes=false;
              else
                   i++;
              raf.seek(i*4);
            }
         }
         catch(EOFExcption e){}
         return yes;
       }
       public void output(int max) throws IOException
       {
          try
              {
                 raf.seek(0);
                 System.out.println("[2.."+max+"]中有 "+(raf.lenght()/4)+" 个素数:");
                 for (int i=0;i<(int)(raf.lenght()/4);i++)
                 {
                    raf.seek(i*4);
                    System.out.print(raf.readInt()+" ");
                    if ((i+1)%10==0) System.out.println();
                 }
              }
          catch(EOFExcption e){}
          System.out.println();
       }
}

Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream) - wolfgangkiefer - 嘎嘎阅读器
 
Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream) - wolfgangkiefer - 嘎嘎阅读器
 


这篇关于Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/579521

相关文章

Java线程池核心参数原理及使用指南

《Java线程池核心参数原理及使用指南》本文详细介绍了Java线程池的基本概念、核心类、核心参数、工作原理、常见类型以及最佳实践,通过理解每个参数的含义和工作原理,可以更好地配置线程池,提高系统性能,... 目录一、线程池概述1.1 什么是线程池1.2 线程池的优势二、线程池核心类三、ThreadPoolE

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

Java调用DeepSeek API的8个高频坑与解决方法

《Java调用DeepSeekAPI的8个高频坑与解决方法》现在大模型开发特别火,DeepSeek因为中文理解好、反应快、还便宜,不少Java开发者都用它,本文整理了最常踩的8个坑,希望对... 目录引言一、坑 1:Token 过期未处理,鉴权异常引发服务中断问题本质典型错误代码解决方案:实现 Token

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Java中ArrayList与顺序表示例详解

《Java中ArrayList与顺序表示例详解》顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构,:本文主要介绍Java中ArrayList与... 目录前言一、Java集合框架核心接口与分类ArrayList二、顺序表数据结构中的顺序表三、常用代码手动