Answer the question
In order to leave comments, you need to log in
How to solve the encoding problem when outputting text to a file from a Java program?
Hello!
Please tell me how to solve the following problem. The program for recording transfusion media and designing different protocols when using them (written in Java) takes lines from a text file (template text in UTF-8 encoding), supplements them with data and outputs them to another text file. The program is written in Eclipse (UTF-8 encoding). In the Eclipse environment itself, the program works without problems: a named text file is created with correctly substituted data. But when creating a "runnable jar file" (and I suspect that the executable will also behave this way), the program starts, but no data is added, and only the original template is output to the file, without calculated data! Only if the template text has ANSI encoding, then the program works with a bang. But there is one "but": part of the template text has Kazakh letters,
Just in case, I give part of the code (I warn you that I'm not a professional programmer, but a doctor, so don't laugh too hard if something goes wrong))):
public static void printEritr(){
if (!dir_Blooder.exists())
dir_Blooder.mkdir();
if (!dir_Base.exists())
dir_Base.mkdir();
File fileWriteIn = new File("C:/Users/Public/Blooder/Base/transit.txt");
File fileReadOut = new File("C:/Users/Public/Blooder/Docs/formEritr.txt");
String line = "";
try {
FileReader fileRead = new FileReader(fileReadOut);
BufferedReader buffReader = new BufferedReader(fileRead);
FileWriter fileWrit = new FileWriter(fileWriteIn);
BufferedWriter buffWriter = new BufferedWriter(fileWrit);
while ((line = buffReader.readLine()) != null) {
if (line.contains("Ф.И.О. больного")) line += " " + name.toUpperCase();
if (line.contains("Дата рождения")) line += " " + Blood_Patient.comboDay[0].getSelectedItem() + " "
+ Blood_Patient.comboMonth[0].getSelectedItem()
+ " " + Blood_Patient.comboYear[0].getSelectedItem() + " года.";
if (line.contains("Дата проведения трансфузии")) {
line += " " + Blood_Patient.comboDay[1].getSelectedItem()+ " " + Blood_Patient.comboMonth[1].getSelectedItem()
+ " " + Blood_Patient.comboYear[1].getSelectedItem() + " года.";
buffWriter.write(line);
buffWriter.newLine();
continue;
}
if (line.contains("начата")) line = " басталды (начата): " + Blood_Patient.startHour + /*Blood_Patient.comboHour.getSelectedItem() +*/ " сағат (час) "
+ Blood_Patient.startMinutes /*Blood_Patient.comboMinute.getSelectedItem()*/ + " минут (минута)";
if (line.contains("окончена")) line = " аяқталды (окончена): " + Blood_Patient.endHour + " сағат (час) " + Blood_Patient.endMinutes + " минут (минута)";
if (line.contains("Группа крови и резус-принадлежность больного")) {
buffWriter.write(line);
buffWriter.newLine();
buffWriter.write(Blood_Patient.comboGroup.getSelectedItem() + "");
buffWriter.newLine(); buffWriter.newLine();
continue;
}
if (line.contains("Отделение больницы")) line += " " + Blood_Patient.valDepart.toUpperCase();
if (line.contains("истории болезни")) line += " " + Blood_Patient.tf_NumOfCase.getText();
if (line.contains("До трансфузии")) line = "Трансфузияға дейін (До трансфузии): АҚ (АД) " + Blood_Patient.indexOfLife[0][0] + " и "
+ Blood_Patient.indexOfLife[0][1] + " мм рт. ст," + " Ps " + Blood_Patient.indexOfLife[0][2]
+ " в минуту," + "T тела " + Blood_Patient.indexOfLife[0][3];
if (line.contains("Трансфузионный анамнез")) line += " " + Blood_Patient.valAnamnes.toUpperCase();
if (line.contains("Показания к трансфузии")) line += " " + Blood_Patient.comboDiagnose.getSelectedItem();
if (line.contains("Наименование трансфузионной среды")) {
buffWriter.write(line);
buffWriter.newLine();
buffWriter.write("Э Р И Т Р О Ц И Т Н А Я В З В Е С Ь");
buffWriter.newLine(); buffWriter.newLine();
continue;
}
if (line.contains("Группа крови и резус-принадлежность донора")) {
buffWriter.write(line);
buffWriter.newLine();
if (Blood_Preparat.groupOfDonor.contains("полож"))
buffWriter.write(Blood_Preparat.groupOfDonor.replace(".", "ительный") + "");
else buffWriter.write(Blood_Preparat.groupOfDonor.replace(".", "ательный") + "");
buffWriter.newLine(); buffWriter.newLine();
continue;
}
if (line.contains("Номер этикетки трансфузионной среды")) line += " " + Blood_Preparat.tf_Code.getText();
if (line.contains("Наименование организации")) {
buffWriter.write(line);
buffWriter.newLine();
buffWriter.write("К А Р А Г А Н Д И Н С К И Й О Б Л А С Т Н О Й Ц Е Н Т Р К Р О В И");
buffWriter.newLine(); buffWriter.newLine();
continue;
}
if (line.contains("Дата заготовки")) line += " " + "09 сентября 1972 года";
if (line.contains("по группе крови")) line += " " + "С О В М Е С Т И М А";
if (line.contains("по резус-фактору")) line += " " + "С О В М Е С Т И М А";
if (line.contains("проводившего пробы")) line += " " + Blood_Patient.nameOfDoctor;
if (line.contains("биологическая проба")) line += " " + "С О В М Е С Т И М А";
if (line.contains("Перелито")) line = "Құйылған (Перелито): " + Blood_Preparat.listPrep.getSelectedValue() + " миллилитр (мл)";
if (line.contains("капельно-струйно – нужное подчеркнуть")) line += " " + Blood_Patient.valInjection.toUpperCase();
if (line.contains("Реакции")) line += " " + Blood_Patient.valComplication.toUpperCase();
if (line.contains("Предпринятые меры")) line += " " + Blood_Patient.valMeasures.toUpperCase();
if (line.contains("проводившего трансфузию")) {
buffWriter.write(line);
buffWriter.newLine();
buffWriter.write(" " + Blood_Patient.nameOfDoctor);
buffWriter.newLine(); buffWriter.newLine();
continue;
}
if (line.contains("После трансфузии")) line = "Трансфузиядан кейін (После трансфузии): АҚ (АД) " + Blood_Patient.indexOfLife[1][0] + " и "
+ Blood_Patient.indexOfLife[1][1] + " мм рт. ст," + " Ps " + Blood_Patient.indexOfLife[1][2]
+ " в минуту," + "T тела " + Blood_Patient.indexOfLife[1][3];
if (line.contains("через 1 час")) line = "1 сағаттан кейін (через 1 час): АҚ (АД) " + Blood_Patient.indexOfLife[2][0] + " и "
+ Blood_Patient.indexOfLife[2][1] + " мм рт. ст," + " Ps " + Blood_Patient.indexOfLife[2][2]
+ " в минуту," + "T тела " + Blood_Patient.indexOfLife[2][3];
if (line.contains("через 2 часа")) line = "2 сағаттан кейін (через 2 часа): АҚ (АД) " + Blood_Patient.indexOfLife[3][0] + " и "
+ Blood_Patient.indexOfLife[3][1] + " мм рт. ст," + " Ps " + Blood_Patient.indexOfLife[3][2]
+ " в минуту," + "T тела " + Blood_Patient.indexOfLife[3][3];
if (line.contains("через 3 часа")) line = "3 сағаттан кейін (через 3 часа): АҚ (АД) " + Blood_Patient.indexOfLife[4][0] + " и "
+ Blood_Patient.indexOfLife[4][1] + " мм рт. ст," + " Ps " + Blood_Patient.indexOfLife[4][2]
+ " в минуту," + "T тела " + Blood_Patient.indexOfLife[4][3];
if (line.contains("Цвет")) line += " светло-желтая";
if (line.contains("Суточный диурез")) line += " " + Blood_Patient.valDiurez + " мл";
if (line.contains("медсестры")) line += " Петровская А.Г.";
if (line.contains("дата, время")) line += " " + Blood_Patient.comboDay[1].getSelectedItem() + " " + Blood_Patient.comboMonth[1].getSelectedItem()+ " "
+ Blood_Patient.comboYear[1].getSelectedItem() + " года, " + Blood_Patient.comboHour.getSelectedItem()
+ ":" + Blood_Patient.comboMinute.getSelectedItem();
buffWriter.write(line);
buffWriter.newLine();
//buffWriter.newLine();
}
buffReader.close();
buffWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar dating = Calendar.getInstance();
SimpleDateFormat formating = new SimpleDateFormat("YYYY-MM-dd, HH-mm-ss");
String timeOfFile = formating.format(dating.getTime()) ;
File fileSave = new File("C:/Users/Public/Blooder/Base/" + name);
if (!fileSave.exists())
fileSave.mkdir();
//File fileInBase = new File("C:/Users/Public/Blooder/Base/" + name +"/" + name + "_" + date.getYear()+ "_" + date.getMonthValue() + "_" + date.getDayOfMonth() + ".txt");
fileWriteIn.renameTo(new File("C:/Users/Public/Blooder/Base/" + name +"/" + name + "_" + timeOfFile + ".txt"));
if (Desktop.isDesktopSupported())
desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.EDIT))
try {
desktop.edit(new File("C:/Users/Public/Blooder/Base/" + name +"/"+ name + "_" + timeOfFile + ".txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
Answer the question
In order to leave comments, you need to log in
Try explicitly specifying the encoding when opening the file
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileReadOut),
"UTF8"
)
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question