[英]File reader deletes the contet of a file
I'm writing a java server and always read requests form the browser. 我正在编写Java服务器,并且总是从浏览器读取请求。 For example, I have in browser http://localhost:8080/C:\\Users\\1\\Desktop\\tur.txt
and read this request. 例如,我在浏览器中拥有http://localhost:8080/C:\\Users\\1\\Desktop\\tur.txt
并阅读此请求。 Then saves a file path. 然后保存一个文件路径。 Then I want to print the contents of the file to browser. 然后,我想将文件的内容打印到浏览器。 For example, some text which is in tur.txt
. 例如, tur.txt
一些文本。
I'm doing it with a method in another class. 我正在用另一个类中的方法来做。
Here is a code of this class: 这是此类的代码:
public class FileReader {
BufferedReader in;
File DataFile;
public void Reader(String directory, PrintStream out) throws IOException {
try {
File stockInputFile = new File(directory);
File StockOutputFile = new File(directory);
FileInputStream fis = new FileInputStream(stockInputFile);
FileOutputStream fos = new FileOutputStream(StockOutputFile);
int count;
if (new File(directory).isDirectory()) {
directory=directory.replace('\\', '/');
out.print("HTTP/1.0 301 Moved Permanently\r\n"+
"Location: /"+directory+"/\r\n\r\n");
out.close();
return;
}
// Open the file (may throw FileNotFoundException)
InputStream f=new FileInputStream(directory);
// Determine the MIME type and print HTTP header
String mimeType="text/plain";
if (directory.endsWith(".html") || directory.endsWith(".htm"))
mimeType="text/html";
else if (directory.endsWith(".jpg") || directory.endsWith(".jpeg"))
mimeType="image/jpeg";
else if (directory.endsWith(".gif"))
mimeType="image/gif";
else if (directory.endsWith(".txt"))
mimeType="text/txt";
else if (directory.endsWith(".class"))
mimeType="application/octet-stream";
out.print("HTTP/1.0 200 OK\r\n"+
"Content-type: "+mimeType+"\r\n\r\n");
System.out.println(mimeType);
// Send file contents to client, then close the connection
byte[] a=new byte[4096];
int n;
while ((n=f.read(a))>0)
out.write(a, 0, n);
// out.flush();
out.close();
}
catch (FileNotFoundException x) {
out.println("HTTP/1.0 404 Not Found\r\n"+
"Content-type: text/html\r\n\r\n"+
"<html><head></head><body>"+directory+" not found</body></html>\n");
out.close();
}
}
}
It takes a directory to find a file a file on disk and PrintStream ( PrintStream out = new PrintStream(new BufferedOutputStream(serSock.getOutputStream()));
) to print the content to browser. 它需要一个目录来找到文件,该文件位于磁盘和PrintStream上( PrintStream out = new PrintStream(new BufferedOutputStream(serSock.getOutputStream()));
)),以将内容打印到浏览器。 But, the problem is that when I read this file. 但是,问题是当我读取此文件时。 The content of this file removes. 该文件的内容将被删除。 That means all that text that I had in tur.txt
deletes. 这意味着我在tur.txt
拥有的所有文本都tur.txt
删除。 And the file becomes emtpty intead of beeng printed to the browser. 并且该文件成为打印到浏览器中的空白。
Can anyone explain why? 谁能解释为什么? Thank you. 谢谢。
The problem was here: 问题出在这里:
FileInputStream fis = new FileInputStream(stockInputFile);
FileOutputStream fos = new FileOutputStream(StockOutputFile);
In FileOutputSteam. 在FileOutputSteam中。 So when I deleted these 2 lines the program started to work correctly. 因此,当我删除这两行时,程序开始正常工作。 I forgot that FileOutputStream clears all the data stored in the file. 我忘记了FileOutputStream会清除文件中存储的所有数据。
I would like to thank @lamsomeone for his help. 我要感谢@lamsomeone的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.