Java:从这个类中创建一个void方法(文本文件转换器)

时间:2013-07-07 13:26:34

标签: java file text methods void

我想做什么:我的类copytest读取文本文件,编辑一个字符并将此新文件保存在新目录中。我想编写一个void-method,它完全相同,然后可以按照以下方式使用:

copy(String "C:\\Old.txt", String "C:\\New.txt", int 1, int 1)

现在复制与我的旧类copytest完全相同,它会读取旧文件,编辑并保存。 我的第一个想法是将两个文件作为第一个参数,但这显然是不可能的。我的新想法是为方法提供旧文件和新文件的两个字符串。它仍然无法正常工作。我希望,你明白,我想做什么以及如何解决这个问题。

旧类代码(有效):

import java.io.*;
import java.util.Scanner;

public class copytest {

    public static void main(String[] args) throws Exception {       
        readFile();
    }

    public static void readFile() throws Exception {

        // Location of file to read
        File file = new File("...old.txt");

        Scanner scanner = new Scanner(file);

        int lineNumber=1;
        int charNumber=1;
        String wantedChar="r";
        int i=0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (i == lineNumber+2) {
                    if (line.length() >= charNumber) {
                        line = line.substring(0,charNumber-1) + wantedChar + line.substring(charNumber);
                    }
                }
                writeFile(line);
                i++;
            }       

        scanner.close();
        System.out.println("File copied."); 
    }

    public static void writeFile(String copyText) throws Exception {

        String newLine = System.getProperty("line.separator");

        // Location of file to output
        Writer output = null;
        File file = new File("...new.txt");
        output = new BufferedWriter(new FileWriter(file, true));
        output.write(copyText);
        output.write(newLine);
        output.close();     
    }

}

新的无效代码(首先尝试使用file作为参数):

public void copy(file old, file new, int x, int y) {

    public static void readFile() throws Exception {

        Scanner scanner = new Scanner(old);

        int lineNumber=y;
        int charNumber=x;
        String wantedChar="r";
        int i=0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (i == lineNumber+2) {
                    if (line.length() >= charNumber) {
                        line = line.substring(0,charNumber-1) + wantedChar + line.substring(charNumber);
                    }
                }
                writeFile(line);
                i++;
            }       

        scanner.close();
        System.out.println("File copied."); 
    }

    public static void writeFile(String copyText) throws Exception {

        String newLine = System.getProperty("line.separator");

        // Location of file to output
        Writer output = null;
        File file = new File(new.getPath());
        output = new BufferedWriter(new FileWriter(file, true));
        output.write(copyText);
        output.write(newLine);
        output.close();     
    }

    readFile();

}

使用字符串作为参数的新尝试,但仍然不起作用:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;


public class copytestnew {


public void copy(String old, String newone, int x, int y) {

        // Location of file to read
        File file = new File(old);

        Scanner scanner = new Scanner(file);

        int lineNumber=y;
        int charNumber=x;
        String wantedChar="r";
        int i=0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (i == lineNumber+2) {
                    if (line.length() >= charNumber) {
                        line = line.substring(0,charNumber-1) + wantedChar + line.substring(charNumber);
                    }
                }
        String newLine = System.getProperty("line.separator");

        // Location of file to output
        Writer output = null;
        File file2 = new File(newone);
        output = new BufferedWriter(new FileWriter(file2, true));
        output.write(line);
        output.write(newLine);
        output.close(); 
                i++;
            }       

        scanner.close();
        System.out.println("File copied"); 
    }

}

2 个答案:

答案 0 :(得分:0)

public void copy(file old, file new, int x, int y) {

    public static void readFile() throws Exception {

您在方法中定义了一个函数。由于java中的所有函数都是方法(静态或非静态),因此不允许这样做。试试这个:

class IDontKnowHowToNameIt {


   public static void copy(file old, file new, int x, int y) {
      //...
      // call readFile from here
      // ...
   }

   private static void readFile() throws Exception {
      //...
   }
}

答案 1 :(得分:0)

我记得你!我上次回答你如何更换其中一条线上的字符。

首先,将decleration更改为

public static void copy(String old, String newone, int x, int y) throws IOException {

注意投掷声明!

现在,当你想要调用这个方法时,你应该在try-catch块中使用它,或者在你复制函数时使用declear抛出异常。