1
I have the following problem in writing a CSV file. The file appears this way:
NULL
My class of Archive
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Arquivo {
private String conteudo;
private BufferedReader leitor;
public static void Gravar(String conteudo) {
try{
File diretorio = new File("C:\\Users\\Desktop\\Teste_Excel\\teste");
BufferedWriter escrever = new BufferedWriter(new FileWriter(diretorio + "arquivoNomes.txt",true));
escrever.append(conteudo + "\n");
escrever.newLine();
escrever.flush();
escrever.close();
}
catch(IOException e){
System.out.println("Erro na gravação do arquivo: "+ e.getMessage());
}
}
public String Ler(String caminho) {
String texto = "";
try {
Reader arquivo = new InputStreamReader(new FileInputStream(caminho), "UTF-8");
leitor = new BufferedReader(arquivo);
while((texto = leitor.readLine()) != null){
this.setConteudo(texto);
System.out.println(this.getConteudo());
}
}catch (Exception e) {
System.out.println("Erro na leitura do arquivo: "+ e.getMessage());
}
return this.conteudo;
}
public void setConteudo(String conteudo) {
this.conteudo = conteudo;
}
public String getConteudo() {
return this.conteudo;
}
}
My Main class
public class Main {
public static void main(String[] args) {
Arquivo aq = new Arquivo();
String caminho = "C:\\Users\\Desktop\\Teste_Excel\\p1.xlsx";
String conteudo = aq.getConteudo();
aq.Ler(caminho);
aq.Gravar(conteudo);
}
}
Have you tried saving the file in CSV to test if the error occurs as well?
– R.Santos
@R.Santosc Yes the file appears with null in the cell.
– William
Try changing this line
BufferedWriter escrever = new BufferedWriter(new FileWriter(diretorio + "arquivoNomes.txt",true));
thereforeBufferedWriter strW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(diretorio + "arquivoNomes.txt",true), StandardCharsets.ISO_8859_1));
– R.Santos
Appears with null.
– William
Excel has its format, it is not simply read as a text file. One can use a library like the Apache POI to read and manipulate the file.
– Renan Gomes
@Renan Really you are right, however the above script is not even worked for csv files.
– William
Try to read the file
xlsx
thus:Workbook workbook = Workbook.getWorkbook(new File("C:\\testes\\teste.xls"));
 Sheet sheet = workbook.getSheet(0);
 int linhas = sheet.getRows();
– R.Santos
@R.Santos am not using external libs like the jexcel api.
– William
You can see if the reading is returning something?
– R.Santos
@R.Santos Reading yes now recording continues to return
null
.– William