当前位置: 技术问答>java相关
在线救助
来源: 互联网 发布时间:2015-06-02
本文导语: import java.io.*; public class counter extends Object { private String currentRecord =null; private String path =null; private BufferedReader file; public static void main(String args[]) { String cont="0"; cont=ReadFile("/count.tx...
import java.io.*;
public class counter extends Object {
private String currentRecord =null;
private String path =null;
private BufferedReader file;
public static void main(String args[]) {
String cont="0";
cont=ReadFile("/count.txt");
WriteFile("count.txt",cont);
}
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path)) ;
String returnStr=null;
try
{
currentRecord = file.readLine();
}
catch(IOException e)
{
System.out.println(e+"读取数据错误");
}
if (currentRecord==null)
currentRecord="没有记录";
else
{
returnStr=currentRecord;
}
return returnStr;
}
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
int Writerstr =Integer.parseInt(counter)+1;
try
{
//?????
//PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
PrintWriter pw = new PrintWriter(new FileOutputStream(path));
pw.println(Writerstr);
pw.close();
}
catch(IOException e)
{
System.out.println(e+"写入文件错误");
}
}
}
编译时出错
1:"counter.java": Error #: 308 : non-static method ReadFile(java.lang.String) cannot be referenced from a static context at line 11, column 7
2:我还想问一个下,在写的代码中我打了问号的地方。两条语句有什么区别
public class counter extends Object {
private String currentRecord =null;
private String path =null;
private BufferedReader file;
public static void main(String args[]) {
String cont="0";
cont=ReadFile("/count.txt");
WriteFile("count.txt",cont);
}
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path)) ;
String returnStr=null;
try
{
currentRecord = file.readLine();
}
catch(IOException e)
{
System.out.println(e+"读取数据错误");
}
if (currentRecord==null)
currentRecord="没有记录";
else
{
returnStr=currentRecord;
}
return returnStr;
}
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
int Writerstr =Integer.parseInt(counter)+1;
try
{
//?????
//PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
PrintWriter pw = new PrintWriter(new FileOutputStream(path));
pw.println(Writerstr);
pw.close();
}
catch(IOException e)
{
System.out.println(e+"写入文件错误");
}
}
}
编译时出错
1:"counter.java": Error #: 308 : non-static method ReadFile(java.lang.String) cannot be referenced from a static context at line 11, column 7
2:我还想问一个下,在写的代码中我打了问号的地方。两条语句有什么区别
|
public static void main(String args[])是java程序的入口点,有了它你程序就可以在一个单独的进程中运行。它并不属于具体哪一个的方法。
所以必需先new counter()然后才能用它的方法。
所以必需先new counter()然后才能用它的方法。