A nice way is to use Filewriter methods to create/edit the file and Filereader to read.
public void gravar() {
FileWriter fileWriter;
BufferedWriter bufferedWriter;
try {
fileWriter = new FileWriter("configuracoes\\config.txt");
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("localhost");
bufferedWriter.close();
fileWriter.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Ocorreu um erro:\n" + ex.getMessage());
}
}
public final void lerArquivo() {
try {
FileReader fileReader = new FileReader("configuracoes\\config.txt");
BufferedReader br = new BufferedReader(fileReader);
try {
setIP(br.readLine());
br.close();
setUrlPorIP(getIP());
} catch (IOException ex) {
BancoDeDados.database.tratamentoDeErro(ex, "");
}
} catch (FileNotFoundException ex) {
new File("configuracoes").mkdir();
File f = new File("configuracoes\\config.txt");
try {
f.createNewFile();
gravarLinhaDeIP();
lerArquivoConfig();
} catch (IOException ex1) {
//JOptionPane.showMessageDialog(null, "O arquivo 'config.txt' não existe.\n"
// + "Foi tentado cria-lo mas algo saiu errado.\n"
// + "Tente criar o arquivo Manualmente");
}
}
}
Documentación Oficial:
http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
http://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
what is your exactly? ta problem with reading a file? of writing the file? could be clearer?
– Math
What kind of union should be done? What is the criterion that determines which entries a text should match with another? Line number? Or some other control value that is written on the lines, like an index or a document number? This can be quite simple or quite complicated to do, agent will only find out if you clarify the problem.
– Havenard
I want to combine content of two. txt, I can read using Scanner and write using Filewriter, it’s more or less the same doubt but I don’t understand the http://stackoverflow.com/questions/10411382/combine-some-elements-from-2-txt-files-into-a-third-file
– haykou
You want to simply paste a text at the end of another?
– Havenard
I have a . txt with 1,Name,RG and another with State,City,Parents , I want to create a third . txt with only the 1,Name,City
– haykou
And what is the common factor between the first and the second text? Is there anything saying which name lives in which city?
– Havenard
There is no necessary factor between the two, I wanted to choose a name of a txt and a city of the other generating a final txt with the union of the two
– haykou
It’s supposed to be random so?
– Havenard
In fact I needed to be able to select each "field" to mount the last
– haykou