텍스트 파일의 특정 줄 번호를 바꿔야합니다. 코드를 작성했는데 작동하지만 몇 줄의 코드가 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를 사용해야합니다.
전에 넣어 == 1 ? 나는 당신의 실수가'tlast '라는 숫자의 모든 인스턴스를'last'의 숫자로 대체하고 있다는 것을 확신합니다. – Gus
나는 게시물에 더 많은 정보를 추가합니다. – seti