Java学习IO流处理

题目

1、在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去;之后遍历IOTest这个目录下的文件

2、统计一个文件中各个字母出现次数包括字符出现次数

3、将一个文本文件内容倒置读出

4、模拟用户登陆控制台输入用户名和密码将用户名密码存储到文件中并随时可以查询所有用户名及密码在模拟用户登录读取文件用户名和密码

5、从控制台输入一个文件名判断所在文件夹是否存在该文件如果不存在则创建该文件如果存在创建一个copy_文件名的文件

6、在当前目录下创建一个文件test.txt”,并向文件输出“Hello World”,如果文件已存在,则在原有文件内容后面追加。

 

解答:

 

第一题:package homework1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class h1 {

    public static void main(String[] args) throws IOException {
        File f=new File("D:\\helloWorld.txt");
        f.createNewFile();
        System.out.println("创建文件成功");
        System.out.println("是文件吗"+f.isFile());
        System.out.println("是目录吗"+f.isDirectory());
        
        File ff=new File("D:\\IOTest");
        ff.mkdir();
        System.out.println("创建目录成功");
        
        File fnew=new File("D:\\IOTest\\helloWorld.txt");
        fnew.createNewFile();
        int i=0;
        
        FileInputStream m=new FileInputStream("D:\\helloWorld.txt");
        FileOutputStream n=new FileOutputStream("D:\\IOTest\\helloWorld.txt");
        
        while((i=m.read())!=-1)
        {
            n.write(i);
        }
        n.close();
        m.close();
        System.out.println("拷贝成功");
    }

}

第二题:
package homework2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class h2 {

    public static void main(String[] args) throws IOException {
        FileInputStream f=new FileInputStream("C:\\Users\\Administrator\\wenjian\\ch.txt");
        int i=0;
        HashMap<Character, Integer> map=new HashMap<>();
        while((i=f.read())!=-1)
        {
            if((48<i&&i<57)||(65<i&&i<90)||(97<i&&i<122))
            {
                    if(map.get((char)i)!=null)
                    {
                        
                        map.put((char)i,map.get((char)i)+1);
                    }
                    else
                    {
                        map.put((char)i,1);
                    }
            }
        }
        Set<Entry<Character, Integer>> entrySet = map.entrySet();
        for(Entry<Character, Integer> l:entrySet)
        {
            System.out.println("字母:"+l.getKey()+",数量:"+l.getValue());
        }
        f.close();
        System.out.println("成功");
    }

}

第三题:
package homework3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class h3 {

    public static void main(String[] args) throws IOException {
        FileInputStream f=new FileInputStream("D:\\h3.txt");
        StringBuilder str=new StringBuilder();
        int i=0;
        while((i=f.read())!=-1)
        {
            str.append((char)i);
            
        }
        System.out.println(str.reverse());
        f.close();

    }
}


第四题:
package homework4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class h4 {

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        HashMap<String, String> map = new HashMap<>();
        Set<Entry<String, String>> entrySet = map.entrySet();

        FileOutputStream f = new FileOutputStream("D:\\h4.txt");

        while (true) {
            map = find(map);
            for (Entry<String, String> l : entrySet) {
                f.write((l.getKey()).getBytes());
                f.write((l.getValue()).getBytes());

            }

            String o = "";
            System.out.print("查看还是继续");
            o = sc.next();
            if (o.equals("查看")) {
                for (Entry<String, String> l : entrySet) {
                    System.out.println(l.getKey() + l.getValue());
                }
                break;
            } else {
                continue;
            }
        }
    }

    private static HashMap<String, String> find(HashMap<String, String> map) {

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入用户名:");
        String user = sc.next();
        System.out.print("请输入用户密码:");
        String key = sc.next();

        map.put(user, key);

        return map;
    }

}

第五题:
package homework5;

import java.awt.event.FocusAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class h5 {

    public static void main(String[] args) throws IOException {
        //人机交互
        System.out.print("请输入要查询的文件名");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();

        
        File f = new File("D:\\");
        File[] files = f.listFiles();
        boolean isok=true;
        
        //遍历D:\\下的文件名,如果有相同的,拷贝,没有就isok设置为false
        for (File ff : files) {
            if (ff.getName().equals(name)) {
                System.out.println("有此文件");
                FileInputStream fis = new FileInputStream("D:\\" + ff.getName());
                FileOutputStream fos = new FileOutputStream("D:\\copy_" + ff.getName());
                int i = 0;
                while ((i = fis.read()) != -1) {
                    fos.write(i);
                }
                System.out.println("重写成功");

                fos.close();
                fis.close();
                isok=false;
                break;
            }
            
        }
        
        //isok为false,新建文件
        if(isok)
        {
            f=new File(f+name);
            f.createNewFile();
            System.out.println("无此文件,创建此文件成功");
        }
        
        System.out.println(f.exists());


}

第六题:
package homework6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class h6 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("./");
        String path = f1.getCanonicalPath();
        File f = new File(path + "\\test.txt");
        if (!f.exists()) {
            f.createNewFile();
            FileOutputStream fos = new FileOutputStream(path + "\\test.txt");
            fos.write("old".getBytes());
            fos.close();
            System.out.println("无此文件,现已创建");
        } else {
            FileInputStream fiss = new FileInputStream(path + "\\test.txt");
            FileOutputStream foss = new FileOutputStream(f,true);

            foss.write("hello".getBytes());
            System.out.println("有此文件,重新写入完毕");
            foss.close();
            fiss.close();

} } }

 

第六题也可以这样写(先读取,再写入)

package homework6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class h6 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("./");
        String path = f1.getCanonicalPath();
        System.out.println(path);
        File f = new File(path + "\\test.txt");
        if (!f.exists()) {
            f.createNewFile();
            FileOutputStream fos = new FileOutputStream(path + "\\test.txt");
            fos.write("old".getBytes());
            fos.close();
            System.out.println("无此文件,现已创建");
        } else {
            FileInputStream fiss = new FileInputStream(path + "\\test.txt");
//            FileOutputStream foss = new FileOutputStream(f,true);
            int i = 0;
            String a="";
            System.out.println(f.getPath());
            while ((i = fiss.read()) != -1) {
                a+=(char)i;
                System.out.println(a);
            }
            FileOutputStream foss = new FileOutputStream(path + "\\test.txt");

            a=a+"hello88";
            System.out.println(a);
            foss.write(a.getBytes());
            System.out.println("有此文件,重新写入完毕");
            foss.close();
            fiss.close();
        }

    }
}

 

Java学习IO流处理

上一篇:Javascript基础


下一篇:JavaWeb-03-Servlet-14-过滤器接口(Filter接口)