我在eclipse中有一个动态web项目。当我正在执行WordFreq.java时,它可以工作。如果我想在MyServlet.java> doGet方法中执行相同的代码,则会出现以下错误:HTTP状态500 - text.txt(系统找不到指定的文件)。 我复制我的文件(的text.txt)无处不在:在java eclipse中动态web项目中没有文件路径
- 该项目的根文件夹
- WebContent文件夹
- src文件夹
- 在我的项目的任何子目录。
仍然会出现此错误。
注意:我的工作空间位于D:/ ....和我的Apache服务器位于C:/ ... 我试图更改为完整路径,但仍然无法工作。
WordFreq.java
package cpd;
import java.io.*;
import java.util.Scanner;
public class WordFreq {
public static void main(String[] args) throws IOException {
File file1 = new File("text.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
String content = new Scanner(file1).useDelimiter("\\Z").next();
CezarBun cezar = new CezarBun();
System.out.println(cezar.criptare(content, 3));
System.out.println("Frecventa literelor");
System.out.println();
int nextChar;
char ch;
double[] count = new double[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') {
count[ch - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c %d", i + 'a', (int) count[i]);
System.out.println("Frecventa: " + count[i]/file1.length() * 100 + "%");
}
in.close();
try {
/*create a buffered reader that connects to the console, we use it so we can
read lines*/
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in2.readLine();
//create an print writer for writing to a file
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
//output to the file a line
out.println(lineFromInput);
//close the stream
out.close();
} catch (IOException e1) {
System.out.println("Error during reading/writing");
}
}
}
MyServlet.java
package cpd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("A pornit servlet-ul" + "<br />");
//inceput wordfreq
File file1 = new File("text.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
String content = new Scanner(file1).useDelimiter("\\Z").next();
CezarBun cezar = new CezarBun();
System.out.println(cezar.criptare(content, 3));
System.out.println("Frecventa literelor");
System.out.println();
int nextChar;
char ch;
double[] count = new double[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') {
count[ch - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c %d", i + 'a', (int) count[i]);
System.out.println(" Frecventa: " + count[i]/file1.length() * 100 + "%");
}
in.close();
try {
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in2.readLine();
//create an print writer for writing to a file
PrintStream out2 = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out2);
//output to the file a line
out2.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out2.close();
} catch (IOException e1) {
System.out.println("Error during reading/writing");
}
//sfarsit wordfreq
// freq = new WordFreqBun();
//freq.wf();
out.print("<br />");
out.print("<a href='http://localhost:8080/CpdApplication/'>Inapoi la meniu</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}