从计算机读取文件并进行编辑并保存为新文件

时间:2015-03-05 15:52:48

标签: java arrays file-io

我正在尝试使用文件中字典的所有单词从我的计算机加载文件。 当我加载文件时,我把单词放入一个字符串数组。 然后我想要删除所有超过9个字母的单词。  然后我想将9个字母或更小的单词保存到另一个单独的文本文件中。  当我尝试打开新文件时,它只有9个单词,但我在eclipse上打印到屏幕上会打印出9个或更少字母的所有单词。  任何人都可以帮忙!

这是一个程序,作为问题的一部分给了我。

      import java.io.*;

        public class FileIO{

          public String[] load(String file) {
            File aFile = new File(file);     
            StringBuffer contents = new StringBuffer();
            BufferedReader input = null;
            try {
              input = new BufferedReader( new FileReader(aFile) );
              String line = null; 
              int i = 0;
              while (( line = input.readLine()) != null){
                contents.append(line);
                i++;
                contents.append(System.getProperty("line.separator"));
              }
            }
            catch (FileNotFoundException ex) {
              System.out.println("Can't find the file - are you sure the file is in this location: "+file);
              ex.printStackTrace();
            }
            catch (IOException ex){
              System.out.println("Input output exception while processing file");
              ex.printStackTrace();
            }
            finally {
              try {
                if (input!= null) {
                  input.close();
                }
              }
              catch (IOException ex) {
                System.out.println("Input output exception while processing file");
                ex.printStackTrace();
              }
            }
            String[] array = contents.toString().split("\n");
            for(String s: array){
                s.trim();
            }
            return array;
          }

 public void save(String file, String[] array) throws FileNotFoundException, IOException {

    File aFile = new File(file); 
    Writer output = null;
    try {
      output = new BufferedWriter( new FileWriter(aFile) );
      for(int i=0;i<array.length;i++){
        output.write( array[i] );
        output.write(System.getProperty("line.separator"));
      }
    }
    finally {
      if (output != null) output.close();
    }
  }
}

这是我尝试使用的课程

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.*;

    public class countdown{

        public static void main(String args[]){

            FileIO reader = new FileIO();
            Scanner scan = new Scanner(System.in);

            String[] inputs = reader.load("C:/Users/Sony/Documents/dict.csv");    //Reading the File as a String array from a file called dict
            String[] input = new String[inputs.length]; //new String array for strings less than 9 letters             
            for(int i=0;i<inputs.length;i++){
                if(inputs[i].length()<=9) {  //if string of index i is less than 9
                  input[i]=inputs[i]; //add it to the new array called input
                  System.out.println(input[i]); //print line to check
                }
            }
            try{
                reader.save("C:/Users/Sony/Documents/dictnew.csv",input);
            //this is where i save it to the new file called dictnew.
            }catch (Exception e){
                System.out.println(e.getClass());
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在阅读完你想要的内容后,你可以分开休息逻辑。

package com.srijan.playground;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FilterLengthWords {

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

        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
             br = new BufferedReader(new FileReader("Sample.txt"));
             bw = new BufferedWriter(new FileWriter("Output.txt"));
             String tmp = null;
             while((tmp=br.readLine())!=null) {
                 if(tmp.length()<=9) {
                     bw.write(tmp);
                 }
             }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(br!=null) {
                br.close();
                br=null;
            }
            if(bw!=null){
                bw.close();
                bw=null;
            }
        }
    }
}

由于