Answer the question
In order to leave comments, you need to log in
The program needs to use Printwriter 2 times. Does the first one work and the second one doesn't?
I am writing a template. There is a large template in which you need to insert data from a file, so you need to do it as many times as there are lines in the Data file. I use Printwriter to write to a file. The first cycle that writes the template header works properly, the second one does not write anything. I can't figure out why. I tried flush(), create a new Printwriter for the second cycle. Does not help. Can someone tell me why. If instead of the second non-working Printwriter loop, output to the console. data is displayed correctly. those. it's the Printwriter.
import java.io.*;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) {
String ShablonCurrentLine;
String DataCurrentLine;
String pathReadShablon = "...\\Shablon.csv";
String pathReadData = "...\\data.csv";
try {
BufferedReader br = new BufferedReader(new FileReader(pathReadShablon)); //Читаем входной шаблон
ShablonCurrentLine = br.readLine();
String[] titleShablonLine = ShablonCurrentLine.split(";");
BufferedReader br1 = new BufferedReader(new FileReader(pathReadData)); //Берем данные для подстановки
while((DataCurrentLine = br1.readLine()) != null){// Построчно берем данные из Data
String[] arrLineData = DataCurrentLine.split(","); // Разделяем строку на массив по разделителю
String path = "C:\\..." + arrLineData[0] + ".csv";
PrintWriter printWriter = new PrintWriter(path);
for (int i = 0; i < titleShablonLine.length; i++) {
printWriter.print(titleShablonLine[i] + ";"); // Тут работает
}
printWriter.println();
while((ShablonCurrentLine = br.readLine()) != null) {// Построчно берем данные из шаблона
String[] arrLineShablon = ShablonCurrentLine.split(";");
for (int i = 0; i < arrLineShablon.length; i++) {
arrLineShablon[i] = arrLineShablon[i].replaceAll("\\{\\{ station_code }}", arrLineData[1]);
arrLineShablon[i] = arrLineShablon[i].replaceAll("\\{\\{ station_ip_addr }}", arrLineData[2]);
arrLineShablon[i] = arrLineShablon[i].replaceAll("\\{\\{ camera_code }}", arrLineData[0]);
arrLineShablon[i] = arrLineShablon[i].replaceAll("\\{\\{ camera_ip_addr }}", arrLineData[3]);
}
for (int i = 0; i < arrLineShablon.length; i++) {
printWriter.print(arrLineShablon[i] + ";");//Тут запись не работает
}
printWriter.println();
}
printWriter.close();
}
br.close();
br1.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question