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 ...
답변
파일을 로컬로 다운로드 한 후 (다운로드 한 후) 다시 FTP를 통해 업로드해야합니다. 또는 서버에있는 경우 File
개체로 열어서 예를 들어 BufferedWriter
으로 작성/추가해야합니다.
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
// Handle exception
}
당신은 그것을 쓰기 위해 파일을 찾아보기 서버의 관점에서 절대/상대 경로를 사용해야합니다!
편집 : 원격 파일 액세스에 대한 자세한 내용은 HERE에서 확인할 수 있습니다.
적
System.out.println(e.toString());
당신은 스택 추적을 풀이 방법 같은 것들을 사용하지 마십시오 출력은 일반적으로 stderr로 전달해야하는 위치 stdout에 간다.
e.printStackTrace();
을 대신 사용하십시오. Btw., 불필요하게 예외를 잡는 것은 더 큰 프로그램에서 큰 문제입니다. Google은 더 많은 것을 배우기 위해 "예외를 삼키는 것"을 내 렸습니다.
안녕하세요 maaartinus! 이것은 질문에 직접 대답하지 않기 때문에 주석이어야합니다. 그럼에도 불구하고, 큰 충고 :) –
물론,하지만 서면의 시점에서 나는 아직 코멘트를 쓸 수 없습니다. 나쁜 예외 처리 /보고가 가장 끔찍한 초보자 실수 중 하나이기 때문에 필자는 그것을 작성하기로 선택했습니다. 다음에는 제대로 할거야. 나는 여기에서 꽤 새로운 사람이다. – maaartinus
가능한 [Java를 사용하여 인터넷에서 파일을 다운로드하고 저장하는 방법]의 복제본 (http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet- using-java) – dogbane
그래서 오류 메시지가 무엇입니까? – Satya
@Satya : 오류가 없습니다. 방금 효과가없는 프로그램을 실행했습니다. – pmad