java怎样高效修改文件部分内容

java怎样高效修改文件部分内容,第1张

整体思路如下:缺汪码

1、用 FileInputStream 读取文件内容

2、修改内容,string *** 作;

3、用 FileOutputStream 写文伏哪件内容;

参考例子陵派如一下:

import java.io.*

public class TestBufferStream{

public static void main(String[] args){

try{

BufferedReader in = new BufferedReader(new FileReader("in.txt"))

BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))

String s = null

while((s = in.readLine()) != null){

out.write(s)

out.newLine()

}

out.flush()

in.close()

out.close()

}catch(IOException e){

e.printStackTrace()

}

}

}

   /**        * this program TODO        * @version         * @ausor widjan wu     雀灶   */        package file        import java io File        import java util ArrayList        import java util Scanner        import ncurrent *        public class ChangeFileName        {        public static void main(String[] args)        {        Scanner in = new Scanner(System in)         System out print( Enter base directory : )         String directory = in nextLine()         System out print( Enter key words: )         String keywords = in nextLine()         ExecutorService pool = Executors newCachedThreadPool()         ChangeName change = new ChangeName(new File(directory) keywords pool)         Future<Integer>result = pool submit(change)         try {        System out println(result get() + files were changed )         } catch (ExecutionException e) {        e printStackTrace()         } catch (InterruptedException e) {        }        pool shutdown()         int largestPoolSize = ((ThreadPoolExecutor) pool) getLargestPoolSize(稿耐)         System out println( largest pool size : + largestPoolSize)         }        }        class ChangeName implements Callable<Integer>        {        public ChangeName(File directory String keywords ExecutorService pool) {        this directory = directory        this pool = pool        this keywords = keywords        }        public Integer call()        {        count =         try        {        File[] files = directory listFiles()         ArrayList<Future<Integer》 results = new ArrayList<Future<Integer》()         for (File file : files) {  键岁春      if (file isDirectory()) {        ChangeName change = new ChangeName(file keywords pool)         Future<Integer>result = pool submit(change)         } else {        count++        String path = file getPath()         int index = path lastIndexOf( \\ )         path = path substring( index + )         System out println(path)         String oldName = file getName()         String fileType = oldName substring(oldName lastIndexOf( ))         String newFName = path + keywords + count + fileType        file renameTo(new File(newFName))         }        }        for(Future<Integer>result:results)        {        try        {        count +=result get()         }catch(ExecutionException e)        {        e printStackTrace()         }        }        }catch(InterruptedException e)        {        }        return count        }        private File directory        private String keywords        private ExecutorService pool        private int count        } lishixinzhi/Article/program/Java/gj/201311/27511

java修改文件名可以直接通过右键文件名“Rename”实现。

第一步裂衡:找到要修改的文件名位悄扮置。

第二步:在文件上右击,选择“Refactor”下的“Rename”。

第三步:输肆运做入新文件名后,点击“确定”即可完成修改 *** 作。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12339725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存