【发布时间】:2014-07-22 21:18:18
【问题描述】:
我一直在尝试用 java 创建一个简单的程序,将一些单词替换到文件中。要将文本更改为我创建的文件,我创建了一个字符串并将其设置为文件文本:
Path path = Paths.get("somePath/someFile.someExtension");
Charset charset = StandardCharsets.UTF_8;
String s = new String(Files.readAllBytes(path), charset);
编辑:要使用s 保存到文件,我使用了Files.write(path, s.getBytes(charset));。
然后我用s.replaceAll("A", "B") 之类的命令更改字符串。但现在,我被困住了。我想做一个更复杂的,然后用“B”替换“A”。我会尽力解释:
我需要在文件中查找wall someNumber someNumer someNumber是否在其中,并且如果有三个参数(someNumber someNumber someNumber),则在中心获取“someNumber”的值。例如:
如果命令是:
wall 200 500 100
wall 200 500 100
然后我想从中心获取参数(在第一种情况下为 500,在第二种情况下为 500),并将其存储到变量中,然后将其从字符串中删除。之后,在这些命令的顶部(在示例中为wall 200 500 100 wall 200 500 100),我想写:
usemtl texture
ceil (someNumber that we stored, in the case, 500)
请注意,如果参数wall wall 没有任何分隔符(例如#other wall),则中间的 someNumber 将相等(500 和 500 相等)。因此,下面的命令将仅按组显示(例如,如果 wall wall wall... 没有与 #other wall 分隔)。
其他示例,这将是之前/之后的文件:
之前:
wall 100 300 50
wall 100 300 100
wall 100 300 400
之后:
usemtl texture
ceil 300
wall 100 50
wall 100 100
wall 100 400
那么,我该如何做这个替换呢?
请回答!我不知道怎么做!
编辑:向@Roan 提问,大部分代码的所有者:
现在,@Roan 的答案代码转换为:
package com.fileConverter.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.JFileChooser;
public class FileReplace extends JFileChooser {
private static final long serialVersionUID = -254322941935132675L;
private static FileReplace chooser = new FileReplace();
public static void main(String[] args) {
chooser.showDialog(chooser, "Open");
}
public void cancelSelection() {
System.exit(0);
}
public void approveSelection() {
super.approveSelection();
System.out.println("starting...");
// The path were your file is
String path = chooser.getSelectedFile().getAbsolutePath();
File file = new File(path);
// try to create an inputstream from the file
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// If we are here the file is not found
e.printStackTrace();
}
// make it a buffered reader
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fis));
// to store the current line
String line;
// array to store the different words
String[] words;
// create a second temporally file that will replace the original file
File file2 = new File(chooser.getSelectedFile().getParentFile()
+ "$$$$$$$$$$$$$$$.tmp");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
// and create the streams
FileOutputStream file2Os = null;
try {
file2Os = new FileOutputStream(file2);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
PrintWriter writer = new PrintWriter(file2Os);
try {
System.out.println("replacing code...");
writer.println("mtllib textures.mtl");
// loop through all lines and
while ((line = bufferedReader.readLine()) != null) {
line = line
.replace("//", "#")
.replace("(", "wall")
.replace(")", "\n")
.replace("{", "")
.replace("}", "")
.replace("# brush from cube",
"room cube" + countWords(line, "cube"))
.replace(" NULL 0 0 0 1 1 0 0 0", "")
.replace("\"classname\"", "")
.replace("\"worldspawn\"", "");
// get all the diffent terms
words = line.split(" ");
// see if there are 4 terms in there: wall x x x
// and if the first term equals wall28
// and if the middle number is the number you want to delete
// if not just copy the line over
if (words.length == 4 && words[0].contains("wall")) {
double doubleVal = Double.parseDouble(words[2]);
int val = (int) doubleVal;
// now modify the line by removing the middel number
String newLine = words[0] + " " + words[1] + " " + words[3];
String valInsert = null;
if (val >= 0)
valInsert = "\n" + "usemtl texture" + "\n" + "ceil "
+ val;
else if (val < 0)
valInsert = "\n" + "usemtl texture" + "\n" + "floor "
+ val;
// write this to the new file
writer.println(valInsert);
writer.println(newLine);
} else {
// copy the old line
writer.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// close our resources
writer.close();
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
// now we rename the temp file and replace the old file
// with the new file with the new content
file.delete();
file2.renameTo(file);
System.out.println("done!");
}
public int countWords(String string, String word) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = string.indexOf(word, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += word.length();
}
}
return count;
}
}
问题是这部分没有做任何替换:
if (words.length == 4 && words[0].contains("wall")) {
double doubleVal = Double.parseDouble(words[2]);
int val = (int) doubleVal;
// now modify the line by removing the middel number
String newLine = words[0] + " " + words[1] + " " + words[3];
String valInsert = null;
if (val >= 0)
valInsert = "\n" + "usemtl texture" + "\n" + "ceil "
+ val;
else if (val < 0)
valInsert = "\n" + "usemtl texture" + "\n" + "floor "
+ val;
// write this to the new file
writer.println(valInsert);
writer.println(newLine);
}
我该如何解决?另一件事,这部分是假设创建一个在检查多维数据集写入次数后增长的数字,但它也不起作用:(
.replace("# brush from cube", "room cube" + countWords(line, "cube"))
countWords 方法:
public int countWords(String string, String word) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = string.indexOf(word, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += word.length();
}
}
return count;
}
非常感谢
【问题讨论】:
-
请澄清您的问题。你想写:PS....
-
是的,我知道它看起来很复杂。我修复了PS,现在尝试再次阅读。有不明白的可以评论,我会一一解答。