IO流:文件输入流、输出流,直接代码解析如何应用。

目录

一、FileInputStream 文件输入流

二、FileOutputStream 文件输出流 

三、FileReader 字符流

四、FileWriter 字符流 


一、FileInputStream 文件输入流

1、单个字节读取文件

   public void readFile01(){
        //文件路径
        String filePath = "d:\\hello.txt";

        //用于接收单个字节
        int readData = 0;

        //初始化FileInputStream,首先置为空
        FileInputStream fileInputStream = null;

        try {

            //创建FileInputStream 对象,用于读取 文件
            fileInputStream = new FileInputStream(filePath);

            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将被阻止
            //如果返回-1,表示读取完毕
            while((readData = fileInputStream.read()) != -1){

                System.out.print((char) readData);//转成char显示

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

2、使用 read(byte[] b) 进行优化

public void readFile02(){
        //文件路径
        String filePath = "d:\\hello.txt";

        //首先定义一个字节数组
        byte[] buf = new byte[8]; //一次性读取8个字节

        //读取长度
        int readLength = 0;

        //初始化FileInputStream ,置为空
        FileInputStream fileInputStream = null;

        try {

            //创建FileInputStream 对象,用于读取 文件
            fileInputStream = new FileInputStream(filePath);

            //从该输入流读取最多b.length字节的数据到字节数组。如果没有输入可用,此方法将被阻止
            //如果返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while((readLength = fileInputStream.read(buf)) != -1){

                System.out.print(new String(buf , 0 , readLength));//显示

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            //关闭文件流,释放资源
            try {

                fileInputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

二、FileOutputStream 文件输出流 

1、创建方式:
  (1)new FileOutputStream(filePath) 的创建方式,当写入内容是会覆盖原来的内容
 
  (2)new FileOutputStream(filePath,true) 的创建方式,当写入内容是,追加到到文件的尾部,不会覆盖

2、代码示例:
public void writeFile(){

        //创建FileOutputStream 对象
        String filePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {

            //得到FileOutputStream 对象
            fileOutputStream = new FileOutputStream(filePath);


            //写入一个字节
            fileOutputStream.write('H');//char 和 int 之间可以自动转换


            //写入字符串
            String str = "hello,world";
            //str.getBytes() 可以把 字符串 -> 字节数组
            fileOutputStream.write(str.getBytes());


            //write(byte[] b,int off,int len) ,将len 字节从位于偏移量off指定字节数组写入
            fileOutputStream.write(str.getBytes(),0,str.length());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {

                fileOutputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

3、文件拷贝代码示例

public class FileCopy {
    public static void main(String[] args) {

        //将d:\\by.png 拷贝到 d:\\bycopy.png
        
        //源文件路径
        String srcFilePath = "d:\\by.png";
        //新文件路径
        String destFilePath = "d:\\bycopy.png";
        
        //初始化FileInputStream 输入流
        FileInputStream fileInputStream = null;
        //初始化FileOutputStream 输出流
        FileOutputStream fileOutputStream = null;

        try {
            //获取输入流对象
            fileInputStream = new FileInputStream(srcFilePath);
            //获取输出流对象
            fileOutputStream = new FileOutputStream(destFilePath);

            //定义一个字节数组,提高效率
            byte[] buf = new byte[1024];
            //获取数组长度
            int readLen = 0;

            while ((readLen = fileInputStream.read(buf)) != -1){

                //读取到后,就写入到文件 通过 fileOutputStream ,即边读边写
                fileOutputStream.write(buf,0,readLen); //一定要使用这个方法

            }
            System.out.println("拷贝成功");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //输入流和输出流都要关闭
            try {
                if (fileInputStream != null){
                    fileInputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

三、FileReader 字符流

1、相关方法:

 

方法功能
new FilleReader(File/String)创建对象
read( )每次读取单个字节,返回该字符,如果到文件末尾返回-1
read( char[] )批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾,则返回-1
new String( char[] )将 char[ ] 转换成String
new String(char[] , off, len)将 char[] 的指定部分转换成String

2、代码示例:单个字符进行读取

    public void readFile01(){
        //文件路径
        String filePath = "d:\\story.txt";

        //初始化对象
        FileReader fileReader = null;

        //接收单个字节
        int data = 0;

        try {
            //1、创建一个FileReader 对象
            fileReader = new FileReader(filePath);

            //循环读取,使用read ,单个字符读取
            while ((data = fileReader.read()) != -1){

                System.out.print((char) data);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

3、代码示例:字符数组进行读取

    public void readFile02(){
        //文件路径
        String filePath = "d:\\story.txt";
        //初始化对象
        FileReader fileReader = null;
        
        //字符长度
        int readLen = 0;
        //创建数组
        char[] buf = new char[8];

        try {
            //1、创建一个FileReader 对象
            fileReader = new FileReader(filePath);

            //循环读取,使用read(buf) ,返回的是实际读取到的字符数
            //如果返回-1,说明文件结束
            while ((readLen = fileReader.read(buf)) != -1){

                System.out.print(new String(buf,0,readLen));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

四、FileWriter 字符流 

1、相关方法

方法功能
new FileWriter(File/String)覆盖模式,相当于流的指针在首端
new FileWriter(File/String , true)追加模式,相当于流的指针在尾端
write(int a)写入单个字符
write(char[] )写入指定数组
write(char[] , off , len)写入指定数组的指定部分
write (string b)写入整个字符串
write(string , off , len)写入字符串的指定部分

2、代码示例

public class FileWriter_ {
    public static void main(String[] args) {
        //文件路径
        String filePath = "d:\\note.txt";

        //创建FileWriter 对象
        FileWriter fileWriter = null;
        
        char[] chars = {'a','b','c'};

        try {
            //获取对象
            fileWriter = new FileWriter(filePath);//默认是覆盖写入

            //write(int):写入单个字符
            fileWriter.write('H');

            //write(char[]): 写入指定数组
            fileWriter.write(chars);

            //write(char[] ,off,len) : 写入指定数组的指定部分
            fileWriter.write(chars , 0 , 2);
            fileWriter.write("努力学习".toCharArray(), 0 , 2);

            //write(String) : 写入整个字符串
            fileWriter.write("努力学习");

            //write(String,off,len) : 写入字符串的指定部分
            fileWriter.write("继续加油", 0 , 2);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                //必须要关闭流,否则无法写入
                //fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
  • 84
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 117
    评论
### 回答1: Unity 是一种跨平台的游戏引擎,可以用于开发各种类型的游戏和交互应用程序。在 Unity 中,我们可以使用 C# 编程语言来访问 IO 以及查找 HID 设备。 IO 是指输入输出,它允许我们从设备读取数据或将数据写入设备。Unity 提供了一些类和方法,使我们能够以编程方式访问和处理 IO 。通过使用 IO ,我们可以读取来自设备的数据,如传感器数据或控制器输入,也可以将数据写入设备,如保存游戏状态或发送指令给外部设备。 HID 设备是指 Human Interface Device,即人机接口设备。它可以是键盘、鼠标、游戏手柄等可用于与计算机进行交互的设备。在 Unity 中,我们可以通过 IO 来查找 HID 设备。通常,我们可以使用 Unity 提供的 `Input` 类来检测和处理用户输入,如键盘按键或鼠标移动。但是,如果我们需要与其他类型的 HID 设备进行交互,我们就需要使用 IO 来查找和访问这些设备。 要通过 IO 找到 HID 设备,我们可以使用 C# 中的 `System.IO` 和 `System.Management` 命名空间中的类和方法。我们可以使用 `ManagementObjectSearcher` 类来搜索系统中的 HID 设备,然后使用 `ManagementObject` 类来获取设备的详细信息。通过分析设备的属性,我们可以确定设备是否是 HID 设备,并进一步与之交互。 总之,Unity 通过 IO 可以找到并与 HID 设备进行交互。我们可以使用 C# 编程语言以及 `System.IO` 和 `System.Management` 命名空间中的类和方法来实现这一目标。通过搜索和分析设备属性,我们可以确定设备是否是 HID 设备,并以及如何与之进行交互。这样,我们就可以为我们的游戏或应用程序添加更加丰富的交互功能。 ### 回答2: Unity通过使用IO的方法可以找到HID设备。 在Unity中,可以使用System.IO命名空间来处理输入输出。要搜索HID设备,我们可以使用System.IO.Directory类的GetFiles方法,该方法接受一个路径参数,并返回该目录中的文件。 首先,我们需要了解HID设备在计算机中的路径。通常,在Windows系统中,HID设备在设备管理器中以类似于"\\.\HID#VID_XXXX&PID_XXXX"的格式表示,其中VID代表供应商ID,PID代表产品ID。因此,我们需要获取所有类似于该格式的设备路径。 这里我们可以编写一个方法来搜索HID设备: ```csharp using System; using System.IO; public class HidDeviceFinder { public static string[] FindHidDevices() { string[] devicePaths = Directory.GetFiles(@"\\.\HID*"); return devicePaths; } } ``` 在上述代码中,我们使用Directory.GetFiles方法来搜索HID设备路径,并将结果以字符串数组的形式返回。 要在Unity中使用这个方法,我们可以在脚本中调用这个方法并打印返回的路径: ```csharp using UnityEngine; public class ExampleScript : MonoBehaviour { void Start() { string[] devicePaths = HidDeviceFinder.FindHidDevices(); foreach (string path in devicePaths) { Debug.Log(path); } } } ``` 这样,在Unity中运行时,控制台将输出找到的HID设备路径。 需要注意的是,在使用IO搜索HID设备时,需要相应的权限和底层操作系统的支持。此外,HID设备的路径可能会根据系统的不同而有所差异,因此我们需要根据实际情况对代码进行调整。 ### 回答3: Unity是一种跨平台的游戏引擎,常用于游戏开发。Unity提供了一些用于读取和写入文件的方法,可以通过IO来访问文件系统。而HID(Human Interface Device)是一种人机接口设备,例如鼠标、键盘和游戏手柄等。 在Unity中,我们可以使用IO来查找HID设备。具体步骤如下: 1. 首先,我们需要导入System.IO和System.Collections.Generic命名空间,以便使用IO和集合。 2. 接下来,我们可以使用System.IO.Directory类的静态方法GetFiles来获取指定文件夹中所有的文件路径。我们可以指定特定的文件夹路径,例如"/dev/input"。 3. 然后,我们可以遍历获取到的文件路径,使用System.IO.File类的静态方法ReadAllLines来读取文件内容。这些文件可能包含有关HID设备的信息。 4. 我们可以使用正则表达式或其他解析方法来筛选并提取我们感兴趣的HID设备信息。例如,我们可以搜索包含特定关键字或特定设备类型的文件。 5. 最后,我们可以将找到的HID设备信息保存到一个集合中,以供后续使用。 总之,Unity通过IO可以找到HID设备的过程是:获取指定文件夹中的文件路径,读取文件内容,筛选和提取HID设备信息,并保存到集合中。这样我们就可以在Unity中获取并使用HID设备了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小黎的培培笔录

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

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

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

打赏作者

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

抵扣说明:

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

余额充值