2014-07-22 6 views
0

텍스트 파일의 특정 줄 번호를 바꿔야합니다. 코드를 작성했는데 작동하지만 몇 줄의 코드가 2 행을 대체합니다. 나쁜 무엇입니까? 내 방법을 사용하지 않고 교체하는 더 빠른 방법이 있습니까? 확인 코드입니다.자바를 사용하여 텍스트 파일의 줄 번호 바꾸기

int last=0; 
Boolean brak=false; 
try{ 

    BufferedReader br = new BufferedReader(new FileReader("last.txt")); 
String strLine; 
int count=0; 
//Read File Line By Line 
while ((strLine = br.readLine()) != null) { 
    // Print the content on the console 
    if(count==1) {last=Integer.parseInt(strLine); 


    } 
    count++; 
} 
//Close the input stream 
br.close(); 
}catch (FileNotFoundException e){//Catch exception if any 
     System.out.println(e.toString()); 
     brak=true; 
    } 
catch (IOException e) { 
     e.printStackTrace();} 
try(Connection c = MsSqlConnect.getConnection()) 
{ 
    System.out.println(last); 

    String SQL = "Select ob_id,ob_dokMagId,dok_NrPelny,ob_TowId,tw_Nazwa,ob_IloscMag,ob_WartBrutto,dok_Typ from dok_Pozycja dp " 
+ "RIGHT JOIN dok__Dokument ON dok_Id=ob_DokMagId " 
+ "LEFT JOIN tw__Towar ON tw_Id=ob_TowId " 
+ "Where ob_TowRodzaj=1 AND dp.ob_id>"+last; 
ResultSet rs = c.createStatement().executeQuery(SQL); 
int tlast=last; 
while(rs.next()){ 
    data.add(new OrderSU(rs.getInt("ob_dokMagId"), rs.getString("dok_NrPelny"), rs.getInt("ob_TowId"), rs.getString("tw_Nazwa"), rs.getInt("ob_IloscMag"), rs.getFloat("ob_WartBrutto"),rs.getInt("dok_Typ"))); 
last=rs.getInt("ob_id"); 
    } 
if(brak==true){ 
    try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("last.txt", true)))) { 
out.print("0"+"\r\n"+last); 
}catch (IOException e) { 
    //exception handling left as an exercise for the reader 
    } 
} 
else 
{ 
    try { 
     System.out.println(last+" "+tlast); 
BufferedReader file = new BufferedReader(new FileReader("last.txt")); 
String line;String input = ""; 
    int count=0; 
    while ((line = file.readLine()) != null){ 
     input += line + "\r\n"; 
      if(count==1) { 
       input = input.replace(Integer.toString(tlast), Integer.toString(last)); 
      } 
      count++; 
     } 

     file.close(); 
     System.out.print(input); 
FileOutputStream File = new FileOutputStream("last.txt"); 
    File.write(input.getBytes()); 
    file.close(); 
} catch (Exception e) { 
    System.out.println("Problem reading file."); 
    } 
} 
c.close(); 

저는이 기능을 2 번 사용합니다. 첫 번째 줄은 DB의 마지막 ID를 저장하고 두 번째 줄은 다른 DB의 마지막 ID를 저장합니다. DB 행을 마지막으로 변경하려면이 ID를 사용해야합니다.

+0

전에 넣어 == 1 ? 나는 당신의 실수가'tlast '라는 숫자의 모든 인스턴스를'last'의 숫자로 대체하고 있다는 것을 확신합니다. – Gus

+0

나는 게시물에 더 많은 정보를 추가합니다. – seti

답변

2

input은 누적됩니다. 즉, 파일에서 5 줄을 읽은 경우 input은 5 줄을 모두 포함합니다. 그래서 만약 당신이 input 5 줄의 텍스트를 포함하고 있기 때문에, 그것은 그 라인의 모든 5에 대한 교체를 수행하는 것, 그 시점에서

input = input.replace(Integer.toString(tlast), Integer.toString(last)); 

말했다.

하나의 입력 줄에서만 바꾸려면 논리를 수정해야합니다.

+0

사실에 대해서 생각하지 않습니다. 나는 내가 필요로하는 것을 바꾸고 입력을 더하기 위해 line nr x을 가져 가야한다고 생각한다. 게시 할 정보를 더 추가했습니다. 이것은 가능 FileNotFoundException 다른 함수가 파일을 닫지 않을 때 catch? – seti

0

마지막 행을 다른 변수에 보관할 수 있습니다. "입력"은 지금까지 읽은 모든 라인을 보유합니다.

0

대신을 시도해보십시오

line = line.replace(Integer.toString(tlast), Integer.toString(last)); 

무조건 모든 라인을 추가하고, 왜 조건부 때 카운트를 제외하고 라인을 추가 할`) (`input.replace을 사용하는 대신 input += line + "\r\n";

관련 문제