java中将file文件对象转string

/**
     * 读取txt文件的内容
     * 
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file)
    {
        String result = "";
        try
        {
            // 构造一个BufferedReader类来读取文件
            // BufferedReader br = new BufferedReader(new FileReader(file));
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file.getPath()), "GBK"));
            String s = null;
            while ((s = br.readLine()) != null)
            {// 使用readLine方法,一次读一行
                result = result + "\n" + s;
            }
            br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // System.out.println(result);
        return result;
    }

你可能感兴趣的:(java,file,txt2String,txt)