首页 > 其他分享 >练习题08File、IO

练习题08File、IO

时间:2022-10-23 22:44:17浏览次数:45  
标签:练习题 java String length IO import new 08File public

1、创建一个文件文件中的内容是
name=张三
age =12 pwd=234
读取文件在控制台打印
张三
12
234

package com.xxx;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class Homework1 {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("d:\\a\\b\\zhangsan.txt");
        byte[] buffer = new byte[1024];
//        String s = "";
        int length = -1;
        while ((length = is.read(buffer)) != -1) {
//            System.out.println(new String(buffer, 0, length));
            String s = new String(buffer, 0, length);
            String[] split = s.split("  ");
            for (int i = 0; i < split.length; i++) {
                int index = split[i].indexOf("=");
                String str = split[i].substring(index + 1);
            System.out.println(str);
            }
//            s += (char)length + "";
//            System.out.println(s);
        }


        is.close();
    }
}

2、使用代码在创建这个一个文件d: \admin\dd\1. txt
向文件中写入admin 12345
再将文件中的内容读取出来只将admin写入到 d:\a\b\2. txt中

package com.xxx;

import java.io.*;

public class Homework2 {
    public static void main(String[] args) throws IOException {
        File file = new File("d:\\a\\b\\111.txt");
        file.createNewFile();

        String s = "admin 12345";
        byte[] bytes = s.getBytes();


        InputStream is = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(is);
        OutputStream os = new FileOutputStream("d:\\a\\b\\222.txt");
        BufferedOutputStream bos = new BufferedOutputStream(os);

        os.write(bytes);

        int length = -1;
        while ((length = bis.read()) != -1) {
            bos.write(length);
        }
    }
}

3、创建一个文件文件中的内容adhdheeuyrydhhddgddhdhddddsvsjssjsjdhdfbfd 将使用高效流读取文件中的内容
存入到1ist集合中统计出a=3 b=2

package com.xxx;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Homework3 {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("d:\\a\\b\\new.txt");
        BufferedInputStream bis = new BufferedInputStream(is);

        List<Character> list = new ArrayList<Character>();
        int length = -1;
        while ((length = bis.read()) != -1) {
//            System.out.print((char)length);
            list.add((char)length);
        }
//        System.out.println(list);

        countOfList(list);
    }

    public static Map<Character,Integer> countOfList(List<Character> list) {
        Map<Character, Integer> map = new HashMap<>();
        list.forEach(c -> {Integer counts = map.get(c);map.put(c,counts == null ? 1 : ++counts); });
        System.out.println(map);
        return map;
    }
}

4、将c盘的音频复制到d盘中

package com.xxx;

import java.io.*;

public class Homework4 {
    public static void main(String[] args) throws IOException {

        InputStream is = new FileInputStream("d:\\a\\b\\old.mp3");
        BufferedInputStream bis = new BufferedInputStream(is);

        OutputStream os = new FileOutputStream("d:\\a\\old.mp3");
        BufferedOutputStream bos = new BufferedOutputStream(os);

        int length = -1;
        while ((length = bis.read()) != -1) {
            bos.write(length);
        }

        bos.close();
        bis.close();
        os.close();
        is.close();

    }
}

标签:练习题,java,String,length,IO,import,new,08File,public
From: https://www.cnblogs.com/wyzel/p/16819883.html

相关文章

  • dilated(dilated cardiomyopathy)
    illnessrelatedtosmoking中,related该怎么理解illnessrelatedtosmoking与吸烟有关的疾病related英[rɪˈleɪtɪd]美[rɪˈletɪd]adj.有关系的;叙述的;vt.叙述(......
  • Python入门练习(Question3)
    完全平方数题目一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?解答defsolution3():n=10whileTrue:n+=1......
  • 【解决】CICD、GitHub actions workflow新建仓库push时报错could not read Username f
    git报错fatal:couldnotreadUsernamefor'https://github.com':Nosuchdeviceoraddress原因是没有GitHubtoken,而且cicd时无法输入用户密码正常来说我们使用act......
  • 练习题07File、Map
    1、请使用Map集合存储自定义数据类型Car做键,对应的价格做值。并使用keySet和entrySet两种方式遍历Map集合。packagecom.xxx;publicclassCar{privateStringna......
  • (四)MySQL基础知识之union和排序
    继续连着昨天数据库的基本操作,今天看下MySQL的union功能和排序 union:UNION操作符用于连接两个以上的SELECT语句的结果组合到一个结果集合中。如果多个SELECT语句会......
  • 刚体模拟1-无约束刚体动力学 Rigid Body Simulation I—Unconstrained Rigid Body Dyn
    介绍这部分课程笔记涉及刚体动力学问题。为了帮助您开始模拟刚体运动,我们提供了实现这些笔记中讨论的大多数概念的代码片段。这部分课程笔记分为两部分。第一部分涵盖了......
  • Lists.partition
    Lists,提供了很多api方便操作。例如:Lists.partition(Listlist,intsize)Lists.partition(Listlist,intsize)将list集合进行切割然后填充到一个List集合里。官方介......
  • #yyds干货盘点#【愚公系列】2022年10月 微信小程序-Behavior
    一、Behavior1.Behavior的定义behaviors是用于组件间代码共享的特性,类似于一些编程语言中的“mixins”或“traits”。每个behavior可以包含一组属性、数据、生命周......
  • C语言的练习题
    有1,2,3,4四个数字,那能组成多少个互不相同且无重复数字的三位数?都是多少?分析:三位数可表示为:个位:g,十位:s,百位:b.可以有多少组合:用for语句的嵌套#include<stdio.h>intmain(......
  • TEMPLATE3. Permutations 排列
    TEMPLATE3.Permutations组合,可以乱序。所以,需要记录哪些数用过了。每次递归时,选用第一个没用过的数。注意回溯时清空标记。//TEMPLATE3.Permutations#include<bits......