gpt4 book ai didi

java - 从另一个文件中更改文件中的特定行

转载 作者:行者123 更新时间:2023-11-29 08:53:15 25 4
gpt4 key购买 nike

所以,到目前为止,我已经仔细阅读了这个论坛上的几乎所有建议,但没有一个能真正帮助我找到我需要的答案。

这是我的主要代码:我省略了工具,因为它工作正常。

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

现在,它并没有改变文件中的任何内容,这是为什么呢?请帮忙。

最佳答案

那是因为您在 Filewriter 类中使用了 equalsIgnoreCase() 并将其与 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

关于java - 从另一个文件中更改文件中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21633387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com