开发者

FileInputStream null error

开发者 https://www.devze.com 2023-03-28 13:50 出处:网络
import java.util.*; import java.io.*; @SuppressWarnings(\"unu开发者_如何学编程sed\") public class search
import java.util.*;
import java.io.*;

@SuppressWarnings("unu开发者_如何学编程sed")
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());
    }
}

}

I'm getting a null error for the file itself not being able to be read, am I doing something wrong? is the file not being read for some reason. I've searched around for awhile and can't seem to figure out what is wrong. any help would be appreciated. Also the inputdata[] string array is up at the top as a global variable but it is there, the problem I'm having is with the reading of the file everything else to my knowledge works. Thanks


Do this for your filename

File data = new File("C:\\data\\sample_in.txt");


    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());
    }
}


If this is a windows system you need to be using back slashes.

File data = new File("C:\\data\\sample_in.txt");

You are getting the null because its not finding the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消