Я хочу прочитать текстовый файл пользовательского ввода (этот файл будет ". java" или ".c ++") во время выполнения и выполнить некоторые вычисления. Я использую MVC архитектуру для реализации этого.
Что я уже сделал;
GetFileServlet
FileService fileService = new FileService();
Files files = new Files();
SizeService sizeService = new SizeService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String value = request.getParameter("code");
File file = new File("C:\\Users\\User\\Desktop\\"+value);
String path = file.toString();
files.setFile(path);
sizeService.getFile();
// Read the URL in the file object
BufferedReader br = new BufferedReader(new FileReader(file));
// Create a empty String variable
// int count = 0;
String text;
// Read the file till it's end
try {
while ((text = br.readLine()) != null) {
//System.out.println(st);
// Display the file in the web page
PrintWriter out = response.getWriter();
out.println(text);
}
files.setCode(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
В этом классе (GetFileServlet) я получаю файл от пользователя и прочитайте весь файл и отправьте значение этого файла и путь к другому классу с именем SizeService. java. В этом классе я могу получить путь к этому файлу, но не могу получить текст из этого файла. Это дает мне нулевое значение. Это значение разбирается через класс с именем Files. java. Когда я передаю текст в этот класс, он печатает текст. Но когда я пытаюсь получить к нему доступ из класса SizeService. java с помощью метода получения, он не печатается. Пожалуйста, помогите мне решить эту проблему.
А также мне нужен лучший способ установить путь к файлу этого файла;
String value = request.getParameter("code");
File file = new File("C:\\Users\\User\\Desktop\\"+value);
Это другие классы;
SizeService. java
public class SizeService {
Files files = new Files();
public String getFile() {
String code = files.getCode();
System.out.println(code + " im in the service"); // This give me a null value
return code;
}
}
Файлы. java
publi c class Files {
private static String file;
private static String code;
public Files() {
super();
// TODO Auto-generated constructor stub
}
public Files(String file, String code) {
super();
Files.code = code;
Files.file = file;
}
public static String getFile() {
return file;
}
public void setFile(String file) {
Files.file = file;
}
public static String getCode() {
return code;
}
public void setCode(String code) {
Files.code = code;
}
}