我有两个班级:
公共类ActUI扩展了javax.swing.JFrame {
//这里还有其他类
private static void writeToFile(java.util.List list, String path) { BufferedWriter out = null; try { File file = new File(path); out = new BufferedWriter(new FileWriter(file, true)); for (Object s : list) { out.write((String) s); out.newLine(); } out.close(); } catch (IOException e) { } UniqueLineReader ULR = new UniqueLineReader(); ULR.setFileName(path); } //there are the other classes here }
公共类UniqueLineReader扩展了BufferedReader {
Set<String> lines = new HashSet<String>(); private Reader arg0; public UniqueLineReader(Reader arg0) { super(arg0); } @Override public String readLine() throws IOException { String uniqueLine; while (lines.add(uniqueLine = super.readLine()) == false); //read until encountering a unique line return uniqueLine; } public void setFileName(String filePath){ try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("test.txt"); UniqueLineReader br = new UniqueLineReader(new InputStreamReader(fstream)); String strLine; // Read File Line By Line PrintWriter outFile2 = new PrintWriter(new File("result.txt")); String result = ""; List data = new ArrayList(); while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println(strLine); data.add(strLine); } writeToFile(data, "result.txt"); // Close the input stream //in.close(); } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); } }
}
我想从actUI中的writeToFile方法访问UniqueLineReader,但是我的代码无法正常工作,我怎么能没有错误呢?请帮帮我。
答案 0 :(得分:0)
看看你的代码。
UniqueLineReader ULR = new UniqueLineReader(); // invalid constructor
ULR.setFileName(path);
没有匹配的构造函数。如果您想从writeToFile()
访问ActUI
,只需将writeToFile()
的访问修饰符更改为公开,现在您可以使用以下
UniqueLineReader.writeToFile(new ArrayList(), path);