java流(steam)io流、InputStream、OutputStream、文件File、

一、简答题

1.java有几种类型的流?java为流提供的抽象类有哪些?
2.你对序列化的理解
3.在你的理解中字符流和字节流有什么区别?

答:
1、java有字符流和字节流两种类型;其中字符流继承于Reader和Writer;字节流继承于InputStream和OutputStream。
2、序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程。
3、字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件

图示1:
字节流与字符流
图示2:
字符流与字节流的区别

二、编程题

(一): 从键盘输入若干个字符,当输入字符“#”时中止输入。统计输入字符的个数,并将他们按输入时的逆序输出。如:
输入:inputstream#
输出:maertstupni
备注:此题我未能完全按照要求做出,输入’#'时未能中止输入,如果各位有什么好的见解,可以评论区评论或者私信,我有意向您请教!

源代码:

import java.io.*;
        import java.util.Scanner;
public class inputStream {
    public static void main(String[] args) throws IOException {
            int count = 0;
        System.out.println("请输入若干字符:");
        Scanner scanner = new Scanner(System.in);
        String a = scanner.nextLine();
        String b = "";
        char[] c = a.toCharArray();
        for (int i = c.length - 1; i >= 0 && a != "#"; i--) {
            b = b + c[i];
            count++;
        }
        System.out.println("逆序输出字符串:");
        System.out.println(b);
        System.out.println("统计输入字符个数:" + count);
        scanner.close();
    }
}

运行结果:
在这里插入代码片

(二):
1、在你的电脑里存在一个文件夹myFile,通过程序在该文件夹中创建一个helloWorld.java文件,并向该文件中随机输入若干个0—9的数字
2、在文件夹myFile中创建一千个随机拓展名为.java, .txt,.doc,文件以产生的顺序命名,如1.java,2.txt等。
3、往myFile中的每个java文件中随机输入若干个‘A’到‘Z’的大写字母

总观代码:
在这里插入图片描述

源代码:

import java.io.*;
import java.util.*;
public class helloWorld {
    public static void main(String[] args) throws IOException, InterruptedException {
        First();
      Second();
      third();
    }
    private static void First() throws IOException {
        File file = new File("E:/我的程序人生/java/java流(Stream)文件和IO/myFile/helloWorld.java");
        file.mkdirs();
       // file.createNewFile();
        FileWriter fw = new FileWriter("E:/我的程序人生/java/java流(Stream)文件和IO/myFile/helloWorld.java/file.txt");
        int len = (int)(Math.random() * 10);
        for(Integer i = 0;i < len ; i++)
        {
            Integer temp = (int)(Math.random() * 10);
            fw.write(temp.toString());
        }
        fw.close();
    }
    private static void Second() throws IOException {
       String filePath = "E:/我的程序人生/java/java流(Stream)文件和IO/myFile/helloWorld.java";
       String fileType1 = ".txt";
       String fileType2 = ".java";
       String fileType3 = ".doc";
       String str = null;
       String path = null;
       for(Integer i=0;i<=1000;i++)
       {
           int temp = (int)(Math.random() * 10);
           if(temp % 3 ==0) {
               str = fileType1;
           }else if(temp %3 == 1) {
               str = fileType2;
           }else if(temp %3 == 2) {
               str = fileType3;
           }
           path = filePath + i.toString() + str;
           File file = new File(path);
           file.createNewFile();
       }
   }
    private static void third() throws IOException {
        File file = new File("E:/我的程序人生/java/java流(Stream)文件和IO/myFile");
        file.mkdir();
        File file1 = new File(file,"file1.txt");
        file1.createNewFile();
        OutputStream out = new FileOutputStream(file1);
        for(int i=0;i<1000;i++)
        {
            int ch = (int)(Math.random() * 26  + 65);
            out.write(ch);
        }
        out.close();
        System.out.println("执行完毕");
        }
    }

运行结果:
在这里插入图片描述

任务运行结果对比图:

1、在你的电脑里存在一个文件夹myFile,通过程序在该文件夹中创建一个helloWorld.java文件,并向该文件中随机输入若干个0—9的数字

在这里插入图片描述

2、在文件夹myFile中创建一千个随机拓展名为.java, .txt,.doc,文件以产生的顺序命名,如1.java,2.txt等。

在这里插入图片描述
3、往myFile中的每个java文件中随机输入若干个‘A’到‘Z’的大写字母

在这里插入图片描述

备注:关于Java流、IO流的更多好方法,希望有机会与各位讨教!
谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值