写了一个读取文件的小程序 虽然程序编译没错 可运行结果总是不能有预期效果
读不到文件路径 也读不出文件内容
请大家帮我查查 出出主意 谢过!
代码如下:public static void getAppInfoFromFile() {
try {
File path = new File(".\\AppInfo\\1.txt");
System.out.println(path.getPath().toString());
FileReader fw = new FileReader(path);
BufferedReader in = new BufferedReader(fw);
String temp = null;
String line = in.readLine();
boolean cont = true;
while (cont) {
if (line == null) {
cont = false;
}
else {
temp += line;
line = in.readLine();
}
System.out.println(temp);
}
in.close();
} catch (IOException e) {
System.out.println("error!");
}
读不到文件路径 也读不出文件内容
请大家帮我查查 出出主意 谢过!
代码如下:public static void getAppInfoFromFile() {
try {
File path = new File(".\\AppInfo\\1.txt");
System.out.println(path.getPath().toString());
FileReader fw = new FileReader(path);
BufferedReader in = new BufferedReader(fw);
String temp = null;
String line = in.readLine();
boolean cont = true;
while (cont) {
if (line == null) {
cont = false;
}
else {
temp += line;
line = in.readLine();
}
System.out.println(temp);
}
in.close();
} catch (IOException e) {
System.out.println("error!");
}
你的要求是什么?
import java.io.*;
class test{
public static void main(String args[])
{
getAppInfoFromFile();
}
public static void getAppInfoFromFile()
{
try
{
File path = new File(".\\AppInfo\\1.txt");
System.out.println(path.getPath().toString());
FileReader fw = new FileReader(path);
BufferedReader in = new BufferedReader(fw);
String temp = null;
String line = in.readLine();
boolean cont = true;
while (cont)
{
if(line == null) {
cont = false;
}
else {
temp += line;
line = in.readLine();
}
System.out.println(temp);
}
in.close();
}
catch (IOException e) {
System.out.println("error!");
}
}
}
编译无错 可总是不出预期结果
不能显示出文件中的内容
麻烦大家给诊断诊断 谢过
public static void getAppInfoFromFile() {
try {
File FileNum = new File(".\\AppInfo");
int length = FileNum.list().length;
System.out.println(FileNum.getPath().toString()); for (int i = 1; i < length; i++) {
File FileRead = new File(".\\AppInfo\\" + i + ".txt"); FileReader fw = new FileReader(FileRead); BufferedReader in = new BufferedReader(fw);
String temp = null;
String line = in.readLine(); boolean cont = true;
while (cont) {
if (line == null) {
cont = false;
}
else {
temp += line;
line = in.readLine();
}
System.out.println(temp);
}
in.close();
}
}
catch (IOException e) {
System.out.println("error!");
} }
或者你用System.getProperty("user.dir")来获取程序工作路径,再组合你的文件路径来获取绝对路径。比如:System.getProperty("user.dir")+"\\AppInfo\\1.txt"