如何使用Java搜索文件并返回值?

时间:2012-02-25 19:15:53

标签: java file loops while-loop return

我需要以下代码的帮助。
我要做的是读取文件中的行数据。 我想这样做的方法是使用while循环遍历这些行来尝试找到特定的“x_y_z”并使用indexOf()方法来查看它是否存在。所以基本上我想运行我的循环,直到我得到一个非-1的值,然后打破循环并返回该值。我无法返回值...我似乎无法将其从循环中取出。谁能在这帮助我?

import java.io.InputStreamReader;
import org.bukkit.entity.Player;

/**
 *
 * @author Tim
 */
public class ReadIn {

    public static int read(String path, int x, int y, int z, Player player) {
        try {
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line
            int index = -1;
            while ((strLine = br.readLine()) != null && index == -1) {
                index = strLine.indexOf(x + "_" + y + "_" + z);
                if (index != -1) {
                    int value = index;
                    break;
                }
            }

            //Close the input stream
            in.close();

        } catch (Exception exception) {//Catch exception if any
            exception.printStackTrace();
        }
    }
}

非常感谢,

3 个答案:

答案 0 :(得分:1)

你的代码没有编译,方法不返回任何值! 这样做:

    public static int read(String path, int x, int y, int z, Player player) {
        int value = -1;
        try {
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine = "";       
            //Read File Line By Line
            int index = -1;
            while ((strLine = br.readLine()) != null) {
                index = strLine.indexOf(x + "_" + y + "_" + z);
                if (index != -1) {
                    value = index;
                    break;
                }
            }
            //Close the input stream
            in.close();
        } catch (Exception exception) {//Catch exception if any
            exception.printStackTrace();
        }
        return value;
    }

值现在将包含正确的索引,否则为-1。

答案 1 :(得分:0)

我看到一些问题..首先,你的函数没有返回,所以这甚至都不会编译。 其次,您将返回一个索引,该索引对应于文件中找到该字符串的某些行的位置。返回整行文本不是更好吗? 当你找到值时,我会把函数从函数返回,而不是使用break。

很抱歉,我现在明白为什么你不在循环中返回。但是,如果你在函数中放入一个finally

,你仍然可以这样做

- 已更新以删除多个退出点 -

- 修正了变量声明,因此它具有最终的范围 -

- 好吧我认为现在应该编译 -

public class ReadIn {

public static int read(String path, int x, int y, int z, Player player)  {
    DataInputStream in = null;
    int index = -1;
    try {
        FileInputStream fstream = new FileInputStream(path);
        in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line            
        while (index == -1 && (strLine = br.readLine()) != null) {
            index = strLine.indexOf(x + "_" + y + "_" + z);
        }
        in.close(); 
    } catch (Exception exception) {//Catch exception if any
        exception.printStackTrace();
    }
    return index;
}

答案 2 :(得分:0)

这应该可以解决您的问题

public class ReadIn {

    public static int read(String path, int x, int y, int z, Player player) {
        try {
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            int value = -1;
            //Read File Line By Line
            int index = -1;
            while ((strLine = br.readLine()) != null) {
                index = strLine.indexOf(x + "_" + y + "_" + z);
                if (index != -1) {
                    value = index;
                    break;
                }
            }

            //Close the input stream
            in.close();
            return value;

        } catch (Exception exception) {//Catch exception if any
            exception.printStackTrace();
        }
        return -1;
    }
}