import java.util.*;
import java.io.*;
@SuppressWarnings("unused")
public class search
{
public static String[] inputdata;
public static String[] routedata;
public static void main(String[] args)
{
inputdata();
routedata();
}
public static void inputdata()
{
String strline;
File data = new File("C:\\data\\sample_in.txt");
int i=0;
try
{
FileInputStream fstream = new FileInputStream(data);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strline = br.readLine()) != null)
{
//inputdata[i]=strline;
System.out.println(strline);
i++;
}
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void routedata()
{
String strline;
File routes = new File("C:\\data\\routes.txt");
int i=0;
try
{
FileInputStream fstream = new FileInputStream(routes);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strline = br.readLine()) != null)
{
//routedata[i]=strline;
i++;
}
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}我得到一个空错误,因为文件本身不能被读取,我做错了什么吗?文件是否由于某种原因未被读取。我已经找了一段时间了,似乎找不出哪里出了问题。任何帮助都将不胜感激。此外,inputdata[]字符串数组在顶部是一个全局变量,但它就在那里,我遇到的问题是读取文件时,据我所知,其他一切都是有效的。谢谢
发布于 2011-08-17 23:04:12
对您的文件名执行此操作
File data = new File("C:\\data\\sample_in.txt");发布于 2011-08-17 23:03:28
如果这是一个windows系统,你需要使用反斜杠。
File data = new File("C:\\data\\sample_in.txt");你得到的是null,因为它找不到文件。
发布于 2011-08-17 23:04:07
public static void inputdata()
{
String strline;
int i=0;
try
{
BufferedReader br = new BufferedReader(new FileReader("C:\\data\\sample_in.txt"));
while ((strline = br.readLine()) != null)
{
inputdata[i]=strline;
i++;
}
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}https://stackoverflow.com/questions/7094969
复制相似问题