AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

per scrivere in un file.

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
music
Guest





PostPosted: Sat Mar 18, 2006 12:12 pm    Post subject: per scrivere in un file. Reply with quote



Questo frammento di codice va bene?

File f = null;
do {
anno = riga.substring(17,19);
f = new File("./files/Transaz."+anno);
if (f.exists()) {
FileWriter f_out = new FileWriter(f);
f_out.write(riga);
f_out.close();
}
riga = in.readLine();
} while (riga != null);
Back to top
Vincent Vega
Guest





PostPosted: Sat Mar 18, 2006 1:12 pm    Post subject: Re: per scrivere in un file. Reply with quote



music ha scritto:

Quote:
Questo frammento di codice va bene?

In che senso?
Back to top
music
Guest





PostPosted: Sat Mar 18, 2006 1:12 pm    Post subject: Re: per scrivere in un file. Reply with quote



Vincent Vega wrote:

Quote:
music ha scritto:

Questo frammento di codice va bene?

In che senso?

devo scrivere in un file (f2) alcuni record contenuti in un altro file (f1).
il problema e' che quando mandavo in esecuzione il programma mi ritrovavo in
f2 solo una riga.
mancava in FileWriter f_out = new FileWriter(f); l'append
con FileWriter f_out = new FileWriter(f, true) funziona regolarmente.
Back to top
Pietro
Guest





PostPosted: Tue Mar 21, 2006 5:12 pm    Post subject: Re: per scrivere in un file. Reply with quote

music ha scritto:
Quote:
Questo frammento di codice va bene?

File f = null;
do {
anno = riga.substring(17,19);
f = new File("./files/Transaz."+anno);
if (f.exists()) {
FileWriter f_out = new FileWriter(f);
f_out.write(riga);
f_out.close();
}
riga = in.readLine();
} while (riga != null);
No che non va bene...

Ci sono almeno 2 errori..
riga perché la leggi dopo? e perché scrivi sul file solo se esiste
cancellando il contenuto? e dov'è il flush()?
Back to top
music
Guest





PostPosted: Wed Mar 22, 2006 12:12 pm    Post subject: Re: per scrivere in un file. Reply with quote

Pietro wrote:

Quote:
music ha scritto:
Questo frammento di codice va bene?

File f = null;
do {
anno = riga.substring(17,19);
f = new File("./files/Transaz."+anno);
if (f.exists()) {
FileWriter f_out = new FileWriter(f);
f_out.write(riga);
f_out.close();
}
riga = in.readLine();
} while (riga != null);
No che non va bene...
Ci sono almeno 2 errori..
riga perché la leggi dopo? e perché scrivi sul file solo se esiste
cancellando il contenuto? e dov'è il flush()?

e' cosi':

FileReader f_in = new FileReader("prova.txt");
BufferedReader in = new BufferedReader(f_in);
String riga = in.readLine();
String anno = null;
File f = null;
try {
do {
anno = riga.substring(17,19);
f = new File("./files/Transaz."+anno);
if (f.exists()) {
FileWriter f_out = new FileWriter(f, true);
f_out.write(riga+"\n");
f_out.close();
}
else {
f.createNewFile();
FileWriter f_out = new FileWriter(f, true);
f_out.write(riga+"\n");
f_out.close();
}
riga = in.readLine();
} while (riga != null);
}
catch(IndexOutOfBoundsException ex){
System.out.println(riga);
}
in.close();
System.out.println("Finished!!!");
}
}

il flush dovrebbe farlo ogni volta che chiude il file no?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.