웹 로그 (텍스트 파일)에 쓰기 Java EE
-
12-12-2019 - |
문제
저는 작동 특정 정보를 저장하기 위해 '작업'당 텍스트 파일을 생성하는 데 실질적인 문제를 해결하고 웹 서버에서 텍스트 파일을 만들 수 없으며, 정보를 저장할 수 없게됩니다.첫 번째 예는 이상적으로 내가 원하는 것 (작성 및 저장 당)
참고 - 현재 테스트 목적을 위해 현재이 작업을 수행하려고합니다.많은 도움을주었습니다.
try {
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("test");
out.close();
} catch (Exception ex) {
System.out.println(ex);
}
.
다른 접근법
try {
URL url;
URLConnection urlConn;
DataOutputStream dos;
DataInputStream dis;
url = new URL("http://localhost:8080/" + request.getContextPath() + "/tmp/myfile.txt");
System.out.println(url);
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty ("Content-Type", "text/plain");
dos = new DataOutputStream (urlConn.getOutputStream());
dos.writeUTF("test");
dos.flush();
dos.close();
//to test
dis = new DataInputStream(urlConn.getInputStream());
String s = dis.readLine();
System.out.println(s);
dis.close();
} catch(Exception ex) {
System.out.println(ex);
}
. 해결책
이렇게하고 싶은 이유가 있는지 모르겠습니다.이미 강력한 로깅 솔루션과 데이터베이스가 있습니다.
첫 번째 경우에는 예외가 발생하지 않으면 파일을 거의 생성합니다. 그 위치를 알지 못합니다.알려진 액세스 가능한 위치에 대한 절대 경로를 사용하거나 .getRealPath
를 사용하여 응용 프로그램 자체와 관련하여 무언가를 사용하십시오.
(전쟁을 배포하는 경우 앱 - 상대 경로에 쓰려고 노력하지는 않음)
두 번째 경우에는 왜 그럴 것이라고 생각하는지 모르겠습니다.
제휴하지 않습니다 StackOverflow