read file from assets

前端 未结 18 2222
终归单人心
终归单人心 2020-11-21 22:47
public class Utils {
    public static List getMessages() {
        //File file = new File(\"file:///android_asset/helloworld.txt\");
        AssetMan         


        
相关标签:
18条回答
  • 2020-11-21 23:46

    Reading and writing files have always been verbose and error-prone. Avoid these answers and just use Okio instead:

    public void readLines(File file) throws IOException {
      try (BufferedSource source = Okio.buffer(Okio.source(file))) {
        for (String line; (line = source.readUtf8Line()) != null; ) {
          if (line.contains("square")) {
            System.out.println(line);
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-21 23:46

    The Scanner class may simplify this.

            StringBuilder sb=new StringBuilder();
            Scanner scanner=null;
            try {
                scanner=new Scanner(getAssets().open("text.txt"));
                while(scanner.hasNextLine()){
                    sb.append(scanner.nextLine());
                    sb.append('\n');
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(scanner!=null){try{scanner.close();}catch (Exception e){}}
            }
            mTextView.setText(sb.toString());
    
    0 讨论(0)
  • 2020-11-21 23:47

    Better late than never.

    I had difficulties reading files line by line in some circumstances. The method below is the best I found, so far, and I recommend it.

    Usage: String yourData = LoadData("YourDataFile.txt");

    Where YourDataFile.txt is assumed to reside in 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;
    
     }
    
    0 讨论(0)
  • 2020-11-21 23:48
    getAssets()
    

    is only works in Activity in other any class you have to use Context for it.

    Make a constructor for Utils class pass reference of activity (ugly way) or context of application as a parameter to it. Using that use getAsset() in your Utils class.

    0 讨论(0)
  • 2020-11-21 23:49
    public String ReadFromfile(String fileName, Context context) {
        StringBuilder returnString = new StringBuilder();
        InputStream fIn = null;
        InputStreamReader isr = null;
        BufferedReader input = null;
        try {
            fIn = context.getResources().getAssets()
                    .open(fileName, Context.MODE_WORLD_READABLE);
            isr = new InputStreamReader(fIn);
            input = new BufferedReader(isr);
            String line = "";
            while ((line = input.readLine()) != null) {
                returnString.append(line);
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {
            try {
                if (isr != null)
                    isr.close();
                if (fIn != null)
                    fIn.close();
                if (input != null)
                    input.close();
            } catch (Exception e2) {
                e2.getMessage();
            }
        }
        return returnString.toString();
    }
    
    0 讨论(0)
  • 2020-11-21 23:49

    Using Kotlin, you can do the following to read a file from assets in Android:

    try {
        val inputStream:InputStream = assets.open("helloworld.txt")
        val inputString = inputStream.bufferedReader().use{it.readText()}
        Log.d(TAG,inputString)
    } catch (e:Exception){
        Log.d(TAG, e.toString())
    }
    
    0 讨论(0)
提交回复
热议问题