try
{
URL url = new URL("http://localhost:8080/Files/textfile.txt");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStream outStream = connection.getOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
objectStream.writeInt(637);
objectStream.writeObject("Hello there");
objectStream.writeObject(new Date());
objectStream.flush();
objectStream.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
i am unable to write text into the file(textfile.txt) . i dn't know wat the problem is?? can anyone explain how to write data to a text file based on url information ...
9365 次
2 に答える
1
ファイルをローカルで (ダウンロード後に) 書き込んでから、FTP 経由で再度アップロードする必要があります。または、サーバー上にある場合は、オブジェクトとして開いてから、たとえばFile
を使用して書き込み/追加する必要があります。BufferedWriter
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
// Handle exception
}
サーバーの観点から絶対/相対パスを使用して、ファイルに書き込むためにファイルを見つける必要があります!
編集: Java HEREでリモート ファイル アクセスの詳細を読むことができます。
于 2011-01-20T10:44:20.290 に答える
0
次のようなものは絶対に使用しないでください
System.out.println(e.toString());
この方法では、スタック トレースが失われ、通常は stderr に出力されるはずの stdout に出力されます。使用する
e.printStackTrace();
代わりは。ところで、どこでも不必要に例外をキャッチすることは、より大きなプログラムでは大きな問題です。詳細については、「例外を飲み込む」をググってください。
于 2011-01-20T11:01:48.577 に答える