[英]Cannot read file from abstract class
I am trying to make a base file plugin which other threads will inherit. 我正在尝试制作其他线程将继承的基本文件插件。 But I am stuck at a point where the file exists and can be read from a normal thread but when I try to read that file from an abstract Base file, it says File not found. 但是我停留在文件存在并且可以从普通线程读取的位置,但是当我尝试从抽象的基本文件读取该文件时,它说“找不到文件”。 Here's my base class :- 这是我的基础课:-
package com.evol.fp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public abstract class BaseFilePlugin extends Thread implements BaseFileReader{
String filename = "";
File file = null;
boolean fileStarted = false;
boolean fileEnded = false;
public BaseFilePlugin() {
file = new File(filename);
}
public void readFile() {
BufferedReader br = null;
System.out.println("Base call: " + filename);
try {
System.out.println("inbside ");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
if(br.readLine().trim().isEmpty()) {
endFile();
return;
} else {
startFile(filename);
String record;
while((record = br.readLine().trim()) != null) {
parseRecord(record);
}
endFile();
}
} catch(Exception ioe) {
ioe.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public abstract void parseRecord(String record);
public void startFile(String filename) {
this.fileStarted = true;
this.fileEnded = false;
}
public void endFile() {
file.delete();
this.fileEnded = true;
this.fileStarted = false;
}
public void run() {
while(true) {
System.out.println("Inside run, fileName: " + filename);
System.out.println("Filestarted: " + fileStarted + ", file exists: " + file.exists());
if(!fileStarted) {
readFile();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @return the filename
*/
public String getFilename() {
return filename;
}
/**
* @param filename the filename to set
*/
public void setFilename(String filename) {
this.filename = filename;
}
}
I am aware of multithreading but never implemented with base class to parse records from a file, if someone tells me what's the problem that will be great. 我知道多线程,但是如果有人告诉我什么是大问题,那我从来没有用基类实现来解析文件中的记录。 I know that the file exists for sure. 我知道该文件确实存在。 Here's my child class: - 这是我的孩子班:-
package com.evol.fp;
public class FileReaderThread extends BaseFilePlugin {
public FileReaderThread() {
super.setFilename("E:\\soham\\soham.txt");
}
@Override
public void parseRecord(String record) {
System.out.println(record);
}
}
But its not calling the child's parseRecord
method, using a simple main method:- 但是它没有使用简单的main方法调用子级的parseRecord
方法:
package com.evol.fp;
public class StartProcess {
public static void main(String[] args) {
FileReaderThread thd = new FileReaderThread();
thd.start();
}
}
I think it's because the parent constructor (BaseFilePlugin.class) is called first before you set your filename in super.setFile("E:\\\\soham\\\\soham.txt");
我认为这是因为在您在super.setFile("E:\\\\soham\\\\soham.txt");
设置文件名之前,首先调用了父构造函数(BaseFilePlugin.class super.setFile("E:\\\\soham\\\\soham.txt");
If you can remove the parent constructor instead and replace your setFileName into setFile where file is iniatilize .eg 如果可以改为删除父构造函数,然后将setFileName替换为setiamize文件所在的setFile,例如
// public BaseFilePlugin() {
// file = new File(filename);
// }
....
....
/**
* @return the file
*/
public String getFile() {
return file
}
/**
* @param file the file to set
*/
public void setFile(String file) {
file = new File(file);
}
and in your subclass 在你的子类中
public FileReaderThread() {
super.setFile("E:\\soham\\soham.txt");
}
BaseFilePlugin
's constructor creates its file
with an empty string since initially String filename = "";
BaseFilePlugin
的构造函数使用空字符串创建其file
,因为最初String filename = "";
. 。
The client calls setFilename(...)
which updates filename
. 客户端调用setFilename(...)
来更新filename
。 However, file
is still the same instance when the object was first created (which is using an empty string as the file name). 但是,第一次创建对象时, file
仍然是同一实例(使用空字符串作为文件名)。
I would suggest to pass the file name as part of the constructor so file
is properly initialized: 我建议将文件名作为构造函数的一部分传递,以便正确初始化file
:
public BaseFilePlugin(String filename) {
this.filename = filename;
file = new File(filename);
}
Optionally, if it makes sense that an instance can read only 1 file, then make those class attributes final, and remove the setFilename()
method. (可选)如果一个实例只能读取1个文件,则将这些类属性设置为final,然后删除setFilename()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.