如何从androidstudio中的资产中读取文本文件?

ve7v8dk2  于 2021-07-08  发布在  Java
关注(0)|答案(4)|浏览(256)

我在android studio的assets文件目录中有may wifi2.txt文件。然而,当我尝试访问它时,我总是得到一个nullpointexception。我的代码如下:(非常感谢)

//CSV FILE READING
    File file = null;

    try {

        FileInputStream is = new FileInputStream(file);

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("wifi2.txt")));
            String line;
            Log.e("Reader Stuff",reader.readLine());
            while ((line = reader.readLine()) != null) {
                Log.e("code",line);
                String[] RowData = line.split(",");
                LatLng centerXY = new LatLng(Double.valueOf(RowData[1]), Double.valueOf(RowData[2]));
                if (RowData.length == 4) {
                    mMap.addMarker(new MarkerOptions().position(centerXY).title(String.valueOf(RowData[0]) + String.valueOf(RowData[3])).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                }

            }

        } catch (IOException ex) {
           ex.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

   //Done with CSV File Reading
s4n0splo

s4n0splo1#

File file = null;
try {
    FileInputStream is = new FileInputStream(file);

实际上,您没有在任何地方使用fileinputstream。就用这段代码

try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("wifi2.txt")));
        String line;
        Log.e("Reader Stuff",reader.readLine());
        while ((line = reader.readLine()) != null) {
            Log.e("code",line);
            String[] RowData = line.split(",");
            LatLng centerXY = new LatLng(Double.valueOf(RowData[1]), Double.valueOf(RowData[2]));
            if (RowData.length == 4) {
                mMap.addMarker(new MarkerOptions().position(centerXY).title(String.valueOf(RowData[0]) + String.valueOf(RowData[3])).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
            }

        }

    } catch (IOException ex) {
       ex.printStackTrace();
    }
2g32fytz

2g32fytz2#

在Kotlin,我们可以做到:-

val string = requireContext().assets.open("wifi2.txt").bufferedReader().use {
                it.readText()
            }
lrpiutwd

lrpiutwd3#

用法: String yourData = LoadData("wifi2.txt"); 假设wifi2.txt位于 assets ```
public String LoadData(String inFile) {
String tContents = "";

try {
    InputStream stream = getAssets().open(inFile);

    int size = stream.available();
    byte[] buffer = new byte[size];
    stream.read(buffer);
    stream.close();
    tContents = new String(buffer);
} catch (IOException e) {
    // Handle exceptions here
}

return tContents;

}

参考
pbossiut

pbossiut4#

从资源中读取文件的方法:

public static String readFile(AssetManager mgr, String path) {
        String contents = "";
        InputStream is = null;
        BufferedReader reader = null;
        try {
            is = mgr.open(path);
            reader = new BufferedReader(new InputStreamReader(is));
            contents = reader.readLine();
            String line = null;
            while ((line = reader.readLine()) != null) {
                contents += '\n' + line;
            }
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignored) {
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) {
                }
            }
        }
        return contents;
    }

相关问题