写入文件 Java – BufferedWriter
Write to file in Java – BufferedWriter
我创建了一个 class (LerEscreverArquivo) 来读取文本文件的内容,对读取的数据进行处理,在屏幕上打印完成处理的结果并在文本文件中写入信息。
屏幕数据读写正常。问题发生在将值写入文本文件时。
脚本只将它写入文件的最后一条记录写入。它忽略以前的记录。它遵循问题的代码和图像。
public class LerEscreverArquivo {
private static final String NomeArquivoEntrada = "E:\DesafioProgramacao\matriculasSemDV.txt";
private static final String NomeArquivoSaida = "E:\DesafioProgramacao\matriculasComDV.txt";
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
try {
//input file
fr = new FileReader(NomeArquivoEntrada);
br = new BufferedReader(fr);
String sCurrentLine;
System.out.println("Início do arquivo.");
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int Total = 0;
int contador = 5;
int resto;
for (int i = 0; i < sCurrentLine.length(); i++) {
int j = Character.digit(sCurrentLine.charAt(i), 10);
Total = Total + (j * contador);
contador = contador - 1;
}
resto = Total / 16;
String decimal = Integer.toHexString(resto);
String DigitoCod=sCurrentLine + "-" + decimal;
//output file
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
bw.write(DigitoCod);
System.out.println(DigitoCod);
}
}
System.out.println("Fim do arquivo.");
} catch (IOException eReader) {
eReader.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
if(bw != null) {
bw.close();
}
if(fw != null) {
fw.close();
}
} catch (IOException exeReader) {
exeReader.printStackTrace();
}
}
}
}
您在循环的每次迭代中初始化 FileWriter
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
bw.write(DigitoCod);
所以基本上你的文件是通过删除以前的内容重新开始的。
尝试将以下两行移动到循环上方,您的问题应该得到解决。
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
编辑
工作代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class LerEscreverArquivo{
private static final String NomeArquivoEntrada = "matriculasSemDV.txt";
private static final String NomeArquivoSaida = "matriculasComDV.txt";
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
try {
// input file
fr = new FileReader(NomeArquivoEntrada);
br = new BufferedReader(fr);
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
String sCurrentLine = "";
System.out.println("Início do arquivo.");
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int Total = 0;
int contador = 5;
int resto;
for (int i = 0; i < sCurrentLine.length(); i++) {
int j = Character.digit(sCurrentLine.charAt(i), 10);
Total = Total + (j * contador);
contador = contador - 1;
}
resto = Total / 16;
String decimal = Integer.toHexString(resto);
String DigitoCod = sCurrentLine + "-" + decimal;
// output file
bw.write(DigitoCod);
System.out.println(DigitoCod);
}
}
System.out.println("Fim do arquivo.");
} catch (IOException eReader) {
eReader.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException exeReader) {
exeReader.printStackTrace();
}
}
}
}
我创建了一个 class (LerEscreverArquivo) 来读取文本文件的内容,对读取的数据进行处理,在屏幕上打印完成处理的结果并在文本文件中写入信息。
屏幕数据读写正常。问题发生在将值写入文本文件时。
脚本只将它写入文件的最后一条记录写入。它忽略以前的记录。它遵循问题的代码和图像。
public class LerEscreverArquivo {
private static final String NomeArquivoEntrada = "E:\DesafioProgramacao\matriculasSemDV.txt";
private static final String NomeArquivoSaida = "E:\DesafioProgramacao\matriculasComDV.txt";
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
try {
//input file
fr = new FileReader(NomeArquivoEntrada);
br = new BufferedReader(fr);
String sCurrentLine;
System.out.println("Início do arquivo.");
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int Total = 0;
int contador = 5;
int resto;
for (int i = 0; i < sCurrentLine.length(); i++) {
int j = Character.digit(sCurrentLine.charAt(i), 10);
Total = Total + (j * contador);
contador = contador - 1;
}
resto = Total / 16;
String decimal = Integer.toHexString(resto);
String DigitoCod=sCurrentLine + "-" + decimal;
//output file
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
bw.write(DigitoCod);
System.out.println(DigitoCod);
}
}
System.out.println("Fim do arquivo.");
} catch (IOException eReader) {
eReader.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
if(bw != null) {
bw.close();
}
if(fw != null) {
fw.close();
}
} catch (IOException exeReader) {
exeReader.printStackTrace();
}
}
}
}
您在循环的每次迭代中初始化 FileWriter
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
bw.write(DigitoCod);
所以基本上你的文件是通过删除以前的内容重新开始的。 尝试将以下两行移动到循环上方,您的问题应该得到解决。
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
编辑
工作代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class LerEscreverArquivo{
private static final String NomeArquivoEntrada = "matriculasSemDV.txt";
private static final String NomeArquivoSaida = "matriculasComDV.txt";
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
try {
// input file
fr = new FileReader(NomeArquivoEntrada);
br = new BufferedReader(fr);
fw = new FileWriter(NomeArquivoSaida);
bw = new BufferedWriter(fw);
String sCurrentLine = "";
System.out.println("Início do arquivo.");
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int Total = 0;
int contador = 5;
int resto;
for (int i = 0; i < sCurrentLine.length(); i++) {
int j = Character.digit(sCurrentLine.charAt(i), 10);
Total = Total + (j * contador);
contador = contador - 1;
}
resto = Total / 16;
String decimal = Integer.toHexString(resto);
String DigitoCod = sCurrentLine + "-" + decimal;
// output file
bw.write(DigitoCod);
System.out.println(DigitoCod);
}
}
System.out.println("Fim do arquivo.");
} catch (IOException eReader) {
eReader.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException exeReader) {
exeReader.printStackTrace();
}
}
}
}