2013-01-08 41 views
0

我目前正在从文本文件中替换一行中的一些字符串。该文件包含以下内容:如何在File Java中保留2个标签空间格式

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(); 
    } 
} 

我得到这样的输出:

output

的CDCD缩进已丢失,它应该从原来的位置,它是在原来的文本文件开始。

有人可以指导我如何解决这个问题。 1代码中的Tabspace检查工作正常,但如何检查2个或更多标签空间。

+0

你到底想达到什么目的?不同之处在于会有'space'和'tab'的组合,你没有正确检查。 – mtk

+1

writer1.write(“\ t”+ replaceValue); < - 当我替换字符串时,您只在此写入一个选项卡 – fge

+0

,它应该从原始位置开始,就像它们在原始文件中一样。看到右侧的图片,CDCD应该从System.out.println(“hi”)开始; ABAB和BCBC保持原来的位置。 – Deathstar

回答

1

我将字符串中的println()语句转换为循环内的char []数组&我检查当前字符是否为转义字符'\ t'(选项卡)。

比方说,所有的字符,直到的“S” ......

System.out.println("My method has been accessed"); 

...是标签,然后存储在一个变量的值等于标签的数量&事后申请的那些数标签。请记住,当您编写'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(); 
     } 
    }  
    }   

} 
+0

是的,它的空间不是标签。但是,你能否给我提供这段代码来计算在第一个字符串“S”出现之前一行中的空格数量。那么当我写入新文件时,它会跳过该空间并开始写入。 – Deathstar

+0

代码已准备就绪。该课程包括我帮助你以前解决的问题和这个问题。这个空格问题用替换字符串和适当的对齐替换下面的语句,因为我测试了它。我没有收到任何评论,以便能够自己理解它。如果它有效,请接受/投票答案:-) –

相关问题