Zdravim.
Nevite nekdo jak se da ukladat text na dalsi radek? Jde mi o to abych po vlozeni nejakeho udaje napsal nejakej prikaz a pak bych zacal zapisovat na dalsi radek(napr. Napisu Nazev VW Golf a rok 1999 dam prikaz DR jako dalsi radek a zacnu na dalsi radek ukladat nove udaje aby vysledkem bylo napr. toto:
*VW Golf 1999
*VW Golf Plus 2004
Ukladam to jako obycejnej text bez grafiky.Toto je cast meho kodu:
...
FileWriter f = null;
try {
f = new FileWriter(nazev+".mtxt"); //<- muj format.
System.out.println("Vlozte udaje.Zapis ukoncete prikazem: EX\n"); //<-EX ukonci program.
String param="";
Scanner scan=new Scanner(System.in);
System.out.println("Nazev a Rok");
f.write(param);
ArrayList a=new ArrayList();
String line;
while((line=scan.nextLine())!=null){
if (line.startsWith("EX")) break; //KONEC ZAPISU.
a.add(line+"\t\t\t\t");
}
int alSize = a.size();
String filmName = null;
for (int i = 0; i < alSize; i++) {
filmName = (String) a.get(i);
f.write(filmName);
//f.newLine();
}
System.out.println("--------------------------end");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
//out.close();
f.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Fórum › Java
Prace s textovym souborem.

Use the PrintWriter, Luke.
Nechce se mi studovat tvůj příklad, proto uvadím jendoduchou aplikaci:
import java.io.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[0])),true);
while(true)
{
try
{ System.out.print("Zadej vetu: ");
String line = in.readLine();
if(line.toLowerCase().equals("x")) break;
out.println(line);
}
catch(IOException e)
{ return;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Je to kvůli tomu, že jméno souboru předávám přes paramrery(args[0]). Takže to spouštěj přes prostředí s paramtry anebo pomocí příkazové řádky. Jinak ten příklad je pouze ilustratuvní a ukazuje jak pracovat s třídou PrintWriter. Jediný důležitý řádek je pro tebe:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[0])),true); NAHRAD args[0] cestou/názvem souboru

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 64 hostů
Podobná vlákna
Práce s textovým souborem v C — založil Tari
Dvourozmerne pole, prace s textovym dokumentem — založil matesomg
Práce se souborem — založil hejnallukas
Práce se souborem v C++ — založil pazdy
Práce se souborem — založil Petr
Moderátoři diskuze