我目前正在从文本文件中替换一行中的某些字符串。该文件包含以下内容:
public class MyC{
public void MyMethod() {
System.out.println("My method has been accessed");
System.out.println("hi");
}
}
我的代码如下:程序只是从数组中定义的特定行替换字符串。我必须保持原来的缩进。
public class ReadFileandReplace {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
boolean l1;
int num[] = {1, 2, 3};
String[] values = new String[]{"AB", "BC", "CD"};
HashMap<Integer,String> lineValueMap = new HashMap();
for(int i=0 ;i<num.length ; i++) {
lineValueMap.put(num[i],values[i]);
}
FileInputStream fs = new FileInputStream("C:\\Users\\Antish\\Desktop\\Test_File.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileWriter writer1 = new FileWriter("C:\\Users\\Antish\\Desktop\\Test_File1.txt");
int count = 1;
String line = br.readLine();
while (line != null) {
l1 = line.contains("\t");
System.out.println(l1);
String replaceValue = lineValueMap.get(count);
if(replaceValue != null) {
if(l1==true){
writer1.write("\t"+replaceValue);}
writer1.write(replaceValue);
} else {
writer1.write(line);
}
writer1.write(System.getProperty("line.separator"));
line = br.readLine();
count++;
}
writer1.flush();
}
}
我得到了这个输出:
CDCD的缩进已丢失,应从原始文本文件中的原始位置开始。
有人可以指导我如何解决这个问题。 1 Tabspace检查代码工作正常,但如何检查2个或更多tabspaces。
答案 0 :(得分:1)
我会将println()语句从string转换为char []数组&amp;在循环中我检查当前char是否是转义字符'\ t'(标签)。
让我们说所有角色直到'S'......
System.out.println("My method has been accessed");
...是标签,然后存储在变量中的值等于标签的数量&amp;然后应用这些数量的标签。请记住,当您编写“Test_File”时,您按下了空格而不是按下标签,您将无法识别出有多少标签。
请将此代码与您的代码进行比较,以遵循Java惯例。
的 CODE:强> 的
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author Deathstar
*/
public class MyC
{
public static void main(String[] args)
{
BufferedReader br = null;
boolean isLineMatched = false;
int c1 = 0, lineNumArrLength, lineCount = 0;
int lineNums[] = {1,2};
char[] toCharArr;
String sCurrentLine, oldText, addSpaces = "";
String[] valuesToOverwrite = new String[] {"AB","BC","CD"};
try
{
lineNumArrLength = lineNums.length;
br = new BufferedReader(new FileReader("C:\\Users\\jtech\\Documents\\NetBeansProjects\\HelpOthers\\src\\textFiles\\Test_File.txt"));
FileWriter writer1 = new FileWriter("C:\\Users\\jtech\\Desktop\\Test_File1.txt");
for (int i = 0;i < (valuesToOverwrite.length -1) ;i++ ) //Loop 3 Times
{
writer1.append(valuesToOverwrite[i]+System.lineSeparator()+System.lineSeparator());
}
while ((sCurrentLine = br.readLine()) != null )
{
oldText = sCurrentLine;
lineCount++;
isLineMatched = false;
toCharArr = sCurrentLine.toCharArray();
while (c1 < lineNumArrLength)
{
if (lineCount == lineNums[c1])
{
for (int c2 = 0; c2 < toCharArr.length; c2++)
{
if (toCharArr[c2] == ' ')
{
addSpaces += " ";
}
}
String newText = sCurrentLine.replace(oldText, addSpaces+valuesToOverwrite[lineCount]);
writer1.append(newText+System.lineSeparator());
isLineMatched = true;
addSpaces = "";
}
c1++;
}
if (isLineMatched == false)
{
writer1.append(oldText+System.lineSeparator());
}
c1 = 0;
}
writer1.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
{
br.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}