当前位置: 技术问答>java相关
在线救助(小问题)
来源: 互联网 发布时间:2015-06-03
本文导语: 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[]) throws FileNotFoundException{ String cont="...
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[]) throws FileNotFoundException{
String cont="0";
counter c = new counter();
cont=c.ReadFile("/count.txt");
c.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+"写入文件错误");
}
}
}
编译成功,运行时却
java.io.FileNotFoundException: /count.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:64)
at java.io.FileReader.(FileReader.java:38)
at untitled7.counter.ReadFile(counter.java:21)
at untitled7.counter.main(counter.java:12)
Exception in thread "main"
public class counter extends Object {
private String currentRecord =null;
private String path =null;
private BufferedReader file;
public static void main(String args[]) throws FileNotFoundException{
String cont="0";
counter c = new counter();
cont=c.ReadFile("/count.txt");
c.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+"写入文件错误");
}
}
}
编译成功,运行时却
java.io.FileNotFoundException: /count.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:64)
at java.io.FileReader.(FileReader.java:38)
at untitled7.counter.ReadFile(counter.java:21)
at untitled7.counter.main(counter.java:12)
Exception in thread "main"
|
没有差别
filePath和path这两个句柄指向同一个对象
另外说一下,"//counter.txt"和"counter.txt"效果应该一样
当然如果你不是我所说的路径问题的话
filePath和path这两个句柄指向同一个对象
另外说一下,"//counter.txt"和"counter.txt"效果应该一样
当然如果你不是我所说的路径问题的话