在JAVA中实现文件读写练习

2021-05-03 22:29

阅读:614

标签:创建   fileinput   buffere   target   void   except   row   ++   pat   

练习1:

有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
根据单词性质动词verb全部存入verb.txt文件中
根据单词性质名词noun全部存入noun.txt文件中
 1 package readandwrite;
 2 /*1.有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
 3         String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
 4         根据单词性质动词verb全部存入verb.txt文件中
 5         根据单词性质名词noun全部存入noun.txt文件中
 6 
 7 */
 8 
 9 import java.io.*;
10 
11 
12 public class FileReadAndWrite {
13     public static void main(String args[]) throws IOException {
14         //WORDS数组
15         String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
16         FileOutputStream outFile1 = new FileOutputStream("verb.txt");
17         FileOutputStream outFile2 = new FileOutputStream("noun.txt");
18         OutputStream out1 = new BufferedOutputStream(outFile1);
19         OutputStream out2 = new BufferedOutputStream(outFile2);
20 
21         for(int i=0;i){
22             if(words[i].startsWith("verb")){
23                 byte[] bytes1 = words[i].getBytes();
24                 out1.write(bytes1);
25             }
26             if(words[i].startsWith("noun")){
27                 byte[] bytes2 = words[i].getBytes();
28                 out2.write(bytes2);
29             }
30         }
31         out1.close();
32         out2.close();
33     }
34 }

练习2:

递归查找指定目录中(包括子目录中),所有的.java文件,
并且,把所有这些找到的java文件,复制到一个指定的目录下;
注意:复制文件包括文件内容*
package readandwrite;

import java.io.*;
public class FileRandW02 {
    public static void main(String args[]) throws IOException {

        findFile(new File("G:\\firstLevel"));

    }
    public static void copyFile(File sourcePath, File targetPath,String fileName) throws IOException {
        FileInputStream inFile = new FileInputStream(sourcePath);
        InputStream inread = new BufferedInputStream(inFile);
        String add="\\";
        String finalWritePath = targetPath.getPath()+add+fileName;
        FileOutputStream ouFile = new FileOutputStream(finalWritePath);//会自动创建,字节流支持任意格式文件
        OutputStream outwrite = new BufferedOutputStream(ouFile);

        byte[] buf = new byte[4096];
        int len;
        while((len=inread.read(buf))!=-1){
            outwrite.write(buf, 0, len);
        }
        inread.close();
        outwrite.close();
    }
    public static void findFile(File currentDir) throws IOException {
        if(currentDir.delete()) return;
        File [] curDirFile = currentDir.listFiles();
        String fileName ;
        for(int i=0;i){
            if(curDirFile[i].isDirectory()) findFile(curDirFile[i]);//递归解决
            if((fileName=curDirFile[i].getName()).endsWith("java")){
                copyFile(new File(curDirFile[i].getPath()),new File("G:\\JavaTestDir\\copyjavahere"),curDirFile[i].getName());
            }
        }
    }
}

 

在JAVA中实现文件读写练习

标签:创建   fileinput   buffere   target   void   except   row   ++   pat   

原文地址:https://www.cnblogs.com/debug-the-heart/p/13196729.html


评论


亲,登录后才可以留言!