基于《java2实用教程》的java知识点复习整理【 第十章——输入、输出流】

第十章——输入、输出流

一、知识结构框架

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NKaTVyAu-1620110515010)(C:\Users\官二的磊子\Desktop\image-20210504143621089.png)]

二、知识点详解

(一)File类

获取文件本身的信息,不涉及对文件的读写操作

1、构造方法:

File(String filename);

File(String directoryPath,String filename);

File(File f, String filename);

2、常用方法:

public String getName() 获取文件的名字。

public boolean canRead() 判断文件是否是可读的。

public boolean canWrite() 判断文件是否可被写入。

public boolean exits() 判断文件是否存在。

public long length() 获取文件的长度(单位是字节)。

public String getAbsolutePath() 获取文件的绝对路径。

public String getParent() 获取文件的父目录。

public boolean isFile() 判断文件是否是一个普通文件,而不是目录。

public boolean isDirectroy() 判断文件是否是一个目录。

public boolean isHidden() 判断文件是否是隐藏文件。

public long lastModified() 获取文件最后修改的时间。

3、创建、运行与删除
import java.io.File;
import java.io.IOException;

public class 文件的创建运行与删除 {
    public static void main(String[] args) throws IOException {
        //创建与删除
        File file1 = new File("bbb.txt");
        file1.createNewFile();
        file1.delete();
        //从idea运行微信
        File file2 = new File("C:\\Program Files (x86)\\Tencent\\WeChat","WeChat.exe");
        Runtime ec = Runtime.getRuntime();
        ec.exec(file2.getAbsolutePath());
    }
}
(二)文件字节输入、输出流
相关构造器与方法:

输入流:FileInputStream(String name); FileInputStream(File file);

读取方法:int read(); int read(byte b[]); int read(byte b[],int off,int len)

关闭流:close();

输出流:FileOutoutStream(String name); FileOutoutStream(File file);

写入方法:void weite(int n); void write(byte b[]); void write(byte b[],int off,int len)

关闭流:close();

1、输入流

四个步骤:

(1)设定输入流的源

(2)创建指向源的输入流

(3)让输入流读取源中的数据

(4)关闭输入流。

例子:

        try {
            //设定输入流的源
            File user = new File("商业计划书.txt");
            //指向源的输入流
            FileInputStream in = new FileInputStream(user);
            //让输入流读取源中的数据
            int t = in.read();
            System.out.println(t);
            //关闭流
            in.close();
        }catch (IOException e){
            e.printStackTrace();
        }
2、输出流

四个步骤:

(1)给出输出流的目的地

(2)创建指向目的地的输出流

(3)让输出流把数据写入到目的地

(4)关闭输出流。

例子:

	 try {
            //创建指向目的地的输出流
            FileOutputStream out = new FileOutputStream("商业计划书.txt");
            //使用输出流写字节
            byte abc[] = "我拿青春陪你赌".getBytes();
            out.write(abc);
            //关闭流
            out.close();

            //向文末加入
            byte b[] = "\n最后你却让我输".getBytes();
            FileOutputStream out2 = new FileOutputStream("商业计划书.txt",true);
            out2.write(b,0,b.length);
            out2.close();

        }catch (Exception e){
            e.printStackTrace();
        }
(三)文件字符输入、输出流
相关构造器与方法:

输入流:FileReader(String name); FileReader(File file);

读取方法:int read(); int read(byte b[]); int read(byte b[],int off,int len)

关闭流:close();

输出流:FileWriter(String name); FileWriter(File file); FileWriter(String name,boolean append); FileWriter(File file,boolean append);

写入方法:void weite(int n); void write(byte b[]); void write(byte b[],int off,int len)

关闭流:close();

例子:
 File sourceFile = new File("商业计划书.txt");
        File targetFile = new File("青春赞歌.txt");
        char c[] = new char[28];
        try {
            //分别创建输入内容的输入流,和写入内容的输出流
            Writer out = new FileWriter(targetFile,true);
            Reader in = new FileReader(sourceFile);
            //读取并写入输入流的内容到指定输出流out
            int n = -1;
            while ((n = in.read(c))!=-1){
                out.write(c,0,n);
            }
            //资源关闭
            out.flush();
            out.close();

        }catch (IOException e){
            e.printStackTrace();
        }
(四)缓冲流
相关构造器与方法:

输入流:BufferedReader(Reader in);

读取方法:readLine(); 读取文本行

相关方法:newLine(); 向文件写入一个回行符

关闭流:close();

输出流:BufferedWriter(Writer out);

写入方法:write(String s,int off,int len); 把字符串s写到文件中

关闭流:close();

(五)随机流
相关构造器与方法:

**随机流:**RandomAccessFile(File file,String mode);

读写方法:

(1)八种基本类型数据对应的读/写方法
boolean readBoolean() void writeBoolean(boolean b)
byte readByte() void writeByte(int n)
char readChar() void writeChar(int n)
short readShort() void writeShort(int n)
int readInt() void writeInt(int n)
long readLong() void writeLong(long l)
float readFloat() void writeDouble(double d)
double readDouble() void writeFloat(float f)
int read() void write(int n)
int readUnsignedByte() int readUnsignedShort()

(2)字节数组、字符串及其他方式的读写方式
int read(byte[] b) / void write(byte[] b) 读/写一个字节数组。
int read(byte[] b,int off,int len) / void write(byte[] b,int off,int len) 从数组的off位置开始,读/写len个字节。
String readUTF() / void writeUTF(String s) 以与机器无关的UTF-8编码方式把str写入文件。
String readLine() 从文本文件中读入一行。
void writeChars(String s) 以字符方式写入一个字符串。
void writeBytes(String s) 以字节方式写入一个字符串。

相关方法:

(3)有关文件位置方法
long getFilePointer() 获得文件的当前位置。
void seek(long pos) 定位文件到pos位置。
int skipBytes(int n) 从当前位置跳过n个字节。
(4)其他方法
void setLength(long newLength) 设置文件长度。
long length() 获得文件的长度。
void close() 关闭文件。
FileChannel getChannel() 获得与该流连接的文件通道对象。
FileDescriptor getFD() 获得与该流连接的文件描述符对象。

例子:
 	try {
            //创建随机流
            RandomAccessFile in = new RandomAccessFile("青春赞歌.txt","rw");
            //读取文件长度
            long length = in.length();
            //设定读取位置
            long position = 0;
            //定位文件到当前位置
            in.seek(position);
            while (position<length){
                //读取
                String str = in.readLine();
                //重新编码
                byte b[] = str.getBytes("iso-8859-1");
                //转换为字符串
                str = new String(b);
                //更新当前读写位置
                position = in.getFilePointer();
                System.out.println(str);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
(六)对象流与序列化

构造方法:ObjectInputStream(InputStream in); ObjectOutputStream(OutputStream out)

相关方法: writeObject(Object obj) 将一个对象obj写入到一个文件; readObject() 读取一个对象到程序中

序列化:一个类如果实现了Serializable接口,那么这个类创建的对象就是所谓序列化的对象。

(八)Scanner解析文件

基本使用框架:

File file = new File("hello.java");
Scanner sc = new Scanner(file);
sc.useDelimiter(正则表达式);

三、题库考查知识点整理

1、BufferedWriter流可以指向FileWriter流。

2、FileOutputStream输出流按字节写出数据。

3、如果程序要写入一个文件,可以创建指向文件的FileWriter输出流。

4、创建FileInputStream 对象,即文件字节输入流可能触发FileNotFoundException异常。

5、FileNotFoundException类是IOException类的子类。

6、FileInputStream流的int read(byte b[]) 方法从源中试图读取b.length个字节到字节数组b中,返回实际读取的字节数目。如果到达文件的末尾,则返回-1。

7、BufferedReader流的源必须是字符输入流(FileReader)。

8、FileOutputStream流顺序地写文件,只要不关闭流,每次调用write方法就顺序地向目的地写入内容,直到流被关闭。

9、FileInputStream流顺序地读取文件,只要不关闭流,每次调用read方法就顺序地读取源中其余的内容,直到源的末尾或流被关闭。

  • 7
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Java中的输入输出是用来处理数据的重要概念。输入用于读取数据,输出用于将数据写入目标位置。下面是有关Java输入输出知识点和示例代码: 1. Java输入 Java中的输入用于从文件、网络连接、标准输入等位置读取数据。常用的输入FileInputStream、BufferedInputStream和DataInputStream等。下面是一个基本的读取文件内容的示例代码: ``` try (FileInputStream fis = new FileInputStream("example.txt")) { int b; while ((b = fis.read()) != -1) { System.out.print((char) b); } } catch (IOException e) { e.printStackTrace(); } ``` 2. Java输出 Java输出用于将数据写入到文件、网络连接、标准输出等位置。常用的输出FileOutputStream、BufferedOutputStream和DataOutputStream等。下面是一个将数据写入文件的示例代码: ``` try (FileOutputStream fos = new FileOutputStream("example.txt")) { String str = "Hello, World!"; byte[] bytes = str.getBytes(); fos.write(bytes); } catch (IOException e) { e.printStackTrace(); } ``` 3. Java缓冲输入输出 Java提供了缓冲输入输出来提高读写效率。缓冲输入输出可以在读写数据时缓存一定量的数据,以减少对底层输入输出的访问次数。常用的缓冲输入输出有BufferedInputStream和BufferedOutputStream等。下面是一个使用缓冲输入输出读写文件的示例代码: ``` try (FileInputStream fis = new FileInputStream("example.txt"); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream("example_copy.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos)) { byte[] bytes = new byte[1024]; int len; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0, len); } } catch (IOException e) { e.printStackTrace(); } ``` 以上就是Java输入输出的基本知识点和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未来村村长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值