zoukankan      html  css  js  c++  java
  • 在JAVA中实现文件读写练习

    练习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<words.length;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<curDirFile.length;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数据类型与运算符
    [DEBUG] Springboot打包jar/war后访问包外的路径
    Html大段文本自适应换行显示-SSM
    [DEBUG] ubuntu pip安装成功却无法import
    [DEBUG] ubuntu mysql root@localhost改了密码还是进不去ERROR 1698 (28000)
    [DEBUG] Spring boot前端html无法下载示例文件
    [DEBUG] spring boot在eclipse中用maven打包成jar访问templates报500错误
    [DEBUG] java中用Runtime调用python 简单程序输出null
    SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
    Numpy安装报错:试过N种安装方法终于
  • 原文地址:https://www.cnblogs.com/debug-the-heart/p/13196729.html
Copyright © 2011-2022 走看看