更改文件中的特定行与另一行

时间:2014-02-07 16:50:56

标签: java class text-files selectionchanging

所以,到目前为止,我已经查看了这个论坛上的每一个建议,但没有一个能真正帮助我找到我需要的答案。

这是我的主要代码: 我已经省去了工具,因为这很好。

public class Management {
    private static int Price1 = 0;
    private static int Price2 = 0;

    public static void main(String[] args) {
        try
        {
            Scanner file = new Scanner(new File("friendsfile"));
            String s;

            while(file.hasNext())
            {
                s = file.nextLine();
                if(s.contains("House 1"))
                {
                    Price1 = getPrice(s);
                }

                if(s.contains("House 2"))
                {
                    Price2 = getPrice(s);
                }
            }
            Filewriter fw = new Filewriter();
            fw.fileWriter(Price1,Price2);
        }
        catch(FileNotFoundException fnfe)
        {
            System.out.println("File not found");
        }
        catch(Exception e)
        {
            System.out.println("HERE IS AN ERROR MATE!");
        }
    }

    private static int getPrice(String s)
    {
        StringTokenizer st = new StringTokenizer(s,";");

        st.nextToken();
        int price1 = Integer.parseInt(st.nextToken().replace(" ",""));
        return price1;
    }
}

现在Filewriter看起来像这样:

public class Filewriter {

    public static void fileWriter(int price1, int price2)
    {
        System.out.println("Ye");
        final String FILE_PATH = "housesfile";
        final String MARKER_LINE1 = "price1";
        final String TEXT_TO_ADD1 = "price1: "+price1;
        final String MARKER_LINE2 = "price2";
        final String TEXT_TO_ADD2 = "price2: "+price2;

        List<String> fileLines = new ArrayList<String>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File(FILE_PATH));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                fileLines.add(line);
                if (line.trim().equalsIgnoreCase(MARKER_LINE1)) {
                    fileLines.add(TEXT_TO_ADD1);
                }
                if (line.trim().equalsIgnoreCase(MARKER_LINE2)) {
                    fileLines.add(TEXT_TO_ADD2);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new File(FILE_PATH));
            for (String line : fileLines) {
                pw.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

}

基本上我想要:

House 1: price; 520592
House 2: price; 342038

来自一个文件的那些价格编号,转换为此文件,这是另一个文件。

house #1
owner: -NAME-
price1: 500000
info: 3 bedrooms, 3 bathrooms.
last changed: - DATE -
rented: no

house #2
owner: -NAME-
price2: 150000
info: 3 bedroom, 3 bathroom.
last changed: -date-
rented: no

现在,它不会改变文件中的任何内容,为什么会这样?请帮忙。

1 个答案:

答案 0 :(得分:0)

这是因为您在equalsIgnoreCase()课程中使用Filewriter并将其与price1price2进行比较。但是,您的文件的值为price1: 500000

因此,将equalsIgnoreCase()更改为contains(),它应该有效。

更好,将两者更改为 大写 ,然后匹配:  line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())

因此,您在Filewrite类中的while循环将是:

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    fileLines.add(line);
    if (line.trim().toLowerCase().contains(MARKER_LINE1.toLowerCase())) {
        fileLines.add(TEXT_TO_ADD1);
    }
    if (line.trim().toLowerCase().contains(MARKER_LINE2.toLowerCase())) {
        fileLines.add(TEXT_TO_ADD2);
    }
}

修改 已尝试并且您的代码有效,但它还包含您之前添加的price1price2值,您可能希望排除/覆盖(在上面修改的if语句中):

house #1
owner: -NAME-
price1: 500000
price1: 520592
info: 3 bedrooms, 3 bathrooms.
last changed: - DATE -
rented: no

house #2
owner: -NAME-
price2: 150000
price2: 342038
info: 3 bedroom, 3 bathroom.
last changed: -date-
rented: no