Java 更改文件中的ID
假设我有这个记事本文件 名称:Company_XXX_768_JGH.txt 内容:Java 更改文件中的ID,java,file,text,path,rename,Java,File,Text,Path,Rename,假设我有这个记事本文件 名称:Company_XXX_768_JGH.txt 内容: Random Text Blah Blah Blah Network ID: 80801568 我需要将ID最后4个数字(1568)更改为(0003) 到目前为止,我能够阅读全文,找到带有数字的行,并打印该行的声明。现在我需要用一个新的数字替换最后4个数字 到目前为止,我得到了这个: -------------------------------在JP的回答之后----------------------
Random Text
Blah Blah Blah
Network ID: 80801568
我需要将ID最后4个数字(1568)更改为(0003)
到目前为止,我能够阅读全文,找到带有数字的行,并打印该行的声明。现在我需要用一个新的数字替换最后4个数字
到目前为止,我得到了这个:
-------------------------------在JP的回答之后------------------------------
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class test {
public static void main(String [] args) throws IOException {
File TEMP = new File("C:\\Users\\Controlled\\Documents\\Company\\E_20150512_101105_0002_80802221_SSH.xml");
boolean fileExists = TEMP.exists();
System.out.println(fileExists);
// The name of the file to open.
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(TEMP);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
List<String> NewTextFile = new ArrayList<String>();
while((line = bufferedReader.readLine()) != null) {
//System.out.println(line);
boolean containsSSH = line.contains("80802221");
if (containsSSH == true)
{
String correctedLine = line.replace("2221","0003");
NewTextFile.add(correctedLine);
System.out.println(correctedLine);
}
else
{
NewTextFile.add(line);
}
}
bufferedReader.close();
// Always close files.
File file = new File("C:\\Users\\Controlled\\Documents\\Company\\Test.xml");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter (fw);
bw.write(line); // How to write a List to a file?
bw.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
TEMP + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ TEMP + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.List;
公开课考试{
公共静态void main(字符串[]args)引发IOException{
File TEMP=新文件(“C:\\Users\\Controlled\\Documents\\Company\\E_20150512_101105_0002_80802221_SSH.xml”);
布尔fileExists=TEMP.exists();
System.out.println(fileExists);
//要打开的文件的名称。
//这将一次引用一行
字符串行=null;
试一试{
//FileReader以默认编码读取文本文件。
文件读取器文件读取器=
新文件读取器(TEMP);
//始终将文件读取器包装在BufferedReader中。
BufferedReader BufferedReader=
新的BufferedReader(文件阅读器);
List NewTextFile=newarraylist();
而((line=bufferedReader.readLine())!=null){
//系统输出打印项次(行);
布尔containsSSH=line.contains(“80802221”);
if(containsSSH==true)
{
字符串更正行=行。替换(“2221”、“0003”);
NewTextFile.add(已更正的行);
系统输出打印LN(校正线);
}
其他的
{
NewTextFile.add(行);
}
}
bufferedReader.close();
//始终关闭文件。
File File=新文件(“C:\\Users\\Controlled\\Documents\\Company\\Test.xml”);
FileWriter fw=新的FileWriter(file.getAbsoluteFile());
//如果文件不存在,则创建它
如果(!file.exists()){
createNewFile();
}
BufferedWriter bw=新的BufferedWriter(fw);
write(line);//如何将列表写入文件?
bw.close();
}
捕获(FileNotFoundException ex){
System.out.println(
“无法打开文件”“+
温度+“'”;
}
捕获(IOEX异常){
System.out.println(
“读取文件“”时出错”
+温度+“'”;
//或者我们可以这样做:
//例如printStackTrace();
}
}
}
将您阅读的每一行存储在列表中(ArrayList或LinkedList都不重要)。按原样存储行,除非containsSSH为true,在这种情况下存储line.replace(“1568”,“0003”)
(字符串由replace调用返回)。
然后关闭读卡器,打开BufferedWriter,并以相同的顺序写回行,不要忘记调用newLine()在每个.Voila!之间,所以我更新了我的代码。由于某些原因,编写工作不起作用。请确保在打开编写器之前关闭读取器,并将新代码添加到问题或打开新问题!这样我现在有了一个列表。我需要将其写入一个文件正确吗?这是For循环吗?For(字符串l:NewTextFile){bw.write(l);bw.newLine();}明白了!谢谢大家。我从这个节目中学到了很多。