อ่านไฟล์จากสินทรัพย์


178
public class Utils {
    public static List<Message> getMessages() {
        //File file = new File("file:///android_asset/helloworld.txt");
        AssetManager assetManager = getAssets();
        InputStream ims = assetManager.open("helloworld.txt");    
     }
}

ฉันกำลังใช้รหัสนี้พยายามอ่านไฟล์จากเนื้อหา ฉันลองทำสองวิธี ก่อนอื่นเมื่อใช้งานที่Fileฉันได้รับFileNotFoundExceptionเมื่อใช้AssetManager getAssets()วิธีไม่เป็นที่รู้จัก มีวิธีแก้ไขปัญหาที่นี่หรือไม่?

คำตอบ:


225

นี่คือสิ่งที่ฉันทำในกิจกรรมสำหรับการอ่านบัฟเฟอร์ / ขยายเพื่อให้ตรงกับความต้องการของคุณ

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));

    // do reading, usually loop until end of file reading  
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

แก้ไข: คำตอบของฉันอาจไร้ประโยชน์หากคำถามของคุณเกี่ยวกับวิธีการทำนอกกิจกรรม หากคำถามของคุณเป็นเพียงวิธีการอ่านไฟล์จากเนื้อหาคำตอบจะอยู่ด้านบน

อัปเดต :

หากต้องการเปิดไฟล์ที่ระบุประเภทเพียงเพิ่มประเภทในการเรียก InputStreamReader ดังต่อไปนี้

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

แก้ไข

ตามที่ @Stan กล่าวในความคิดเห็นรหัสที่ฉันให้ไม่ได้รวมบรรทัด mLineจะถูกแทนที่ทุกรอบ //process lineนั่นเป็นเหตุผลที่ผมเขียน ฉันถือว่าไฟล์มีข้อมูลบางประเภท (เช่นรายชื่อผู้ติดต่อ) และแต่ละบรรทัดควรประมวลผลแยกกัน

ในกรณีที่คุณต้องการโหลดไฟล์โดยไม่มีการประมวลผลใด ๆ คุณจะต้องสรุปผลรวมmLineในแต่ละรอบการใช้StringBuilder()และการต่อท้ายแต่ละรอบ

แก้ไขอีกต่อไป

จากความคิดเห็นของ @Vincent ฉันได้เพิ่มfinallyบล็อกนี้

โปรดทราบว่าใน Java 7 และสูงกว่าคุณสามารถใช้try-with-resourcesเพื่อใช้AutoCloseableและCloseableคุณสมบัติของ Java ล่าสุด

บริบท

ในจุดที่ความคิดเห็น @LunarWatcher ให้เห็นว่าgetAssets()เป็นในclass contextดังนั้นถ้าคุณเรียกมันว่านอกactivityคุณจำเป็นต้องอ้างถึงมันและส่งผ่านอินสแตนซ์บริบทของกิจกรรม

ContextInstance.getAssets();

นี่คือคำอธิบายในคำตอบของ @Maneesh ดังนั้นหากสิ่งนี้มีประโยชน์กับคุณถอนคำตอบของเขาเพราะเขาเป็นคนที่ชี้ให้เห็น


2
@Stan จากนั้นเขียนเกี่ยวกับมันในความคิดเห็นและให้ผู้เขียนตัดสินใจว่าพวกเขาต้องการอัปเดตหรือไม่ การแก้ไขมีไว้เพื่อปรับปรุงความชัดเจนไม่ใช่การเปลี่ยนความหมาย การแก้ไขรหัสควรโพสต์เป็นความคิดเห็นก่อนเสมอ
KyleMit

2
รหัสของคุณไม่รับประกันว่าจะปิดสตรีมและเพิ่มทรัพยากรในเวลาที่เหมาะสม finally {reader.close();}ผมขอแนะนำให้คุณใช้
Vincent Cantin

2
ฉันคิดว่ามันมีประโยชน์ที่จะชี้ให้เห็นว่ารหัสข้างต้นแสดงข้อผิดพลาดใน ADT - "reader.close ();" บรรทัดจะต้องใส่ในบล็อกลองอีกอัน ตรวจสอบกระทู้นี้: stackoverflow.com/questions/8981589/… :)
JakeP

1
getAssets เป็นคลาสในบริบทดังนั้นสำหรับการใช้งานนอกกิจกรรมจะต้องทำการเรียกบริบท ดังนั้นนอกกิจกรรมมันจะเป็นอย่างไรcontext.getAssets(.....)
Zoe

1
ตามการอัปเดตของคุณ (ขอบคุณสำหรับการเพิ่ม btw นั้น) การมีบริบทในฟิลด์แบบสแตติกเป็นหน่วยความจำรั่ว ควรใช้ด้วยความระมัดระวังและทำความสะอาดอย่างเหมาะสม มิฉะนั้นคุณจะจบลงด้วยการรั่วไหลของหน่วยความจำที่มีผลกระทบอย่างมากต่อแอพ
Zoe

65
getAssets()

ใช้งานได้ในกิจกรรมในชั้นเรียนอื่น ๆ ที่คุณต้องใช้Contextเท่านั้น

สร้างConstructor สำหรับการอ้างอิงคลาสUtilsของกิจกรรม (วิธีที่น่าเกลียด) หรือบริบทของแอปพลิเคชันเป็นพารามิเตอร์ให้กับมัน การใช้นั้นใช้ getAsset () ในคลาส Utils ของคุณ


1
มันทำงานกับทุกสิ่งที่เป็นประเภทย่อยของบริบทซึ่งกิจกรรมเป็นหนึ่งในหลาย ๆ
Jeremy Logan

Contextเพียงแค่ตั้งข้อสังเกตผมได้เขียน
user370305

@ user370305 คุณรู้หรือไม่ว่าฉันจะแปลง InputStream เป็น FileInputStream ได้อย่างไร
hotHead

@ user370305 ด้วยวิธีนี้น่าเกลียด?
Sevastyan Savanyuk

การส่งผ่านวัตถุ UI โดยไม่คำนึงถึงผลที่ตามมามักจะเป็นวิธีปฏิบัติที่ไม่ดี หากคุณไม่ระวังคุณสามารถทำให้หน่วยความจำรั่วไหลหรือพยายามใช้บริบทที่ตายแล้ว อ่านได้ดีในหัวข้อ: android.jlelse.eu/memory-leak-patterns-in-android-4741a7fcb570
milosmns

49

มาสายดีกว่าไม่มาเลย.

ฉันมีปัญหาในการอ่านไฟล์ทีละบรรทัดในบางสถานการณ์ วิธีการด้านล่างเป็นวิธีที่ดีที่สุดที่ฉันเคยพบมาและฉันแนะนำ

การใช้งาน: String yourData = LoadData("YourDataFile.txt");

ตำแหน่งที่YourDataFile.txtคาดว่าจะอยู่ในสินทรัพย์ /

 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;

 }

สตริงส่งคืนของฉันคือ android.content.res.AssetManager$AssetInputStream@4195dfa0 ..
Boldijar Paul

เหมือนกันที่นี่ res.AssetManager $ AssetInputStream @ .... สาเหตุใดที่ทำให้ส่งคืนได้
Bigs

คุณจัดสรรหน่วยความจำสองครั้ง - ก่อนอื่นสำหรับบัฟเฟอร์และจากนั้นสำหรับสตริง ไม่ทำงานกับไฟล์ที่ใหญ่กว่า
JaakL

1
วิธีที่สมบูรณ์แบบที่จะจัดสรรขนาด stream.available()buffer
Kasim Rangwala

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

คุณจะคิดว่าถ้าคุณปิด BufferedReader มันจะต้องปิด InputStreanReader และ InputStream โดยอัตโนมัติเช่นกัน input = new BufferedReader(new InputStreamReader(fIn));เพราะสิ่งที่คุณไม่ได้สร้างการจัดการสำหรับผู้ที่เช่น
ทรานส์

3
ฉันขอแนะนำให้สร้างบล็อก try / catch แยกต่างหากเพื่อปิดทรัพยากรทั้งหมดของคุณในตอนท้าย แทนที่จะรวมพวกมันทั้งหมดเข้าด้วยกันเพราะมันอาจปล่อยให้ทรัพยากรอื่นไม่ได้ถูกเปิดเผยหากความพยายามก่อนหน้านี้เพื่อปิดแหล่งข้อมูลอื่นทำให้เกิดข้อยกเว้น
Reece


8

โซลูชันบรรทัดเดียวสำหรับ kotlin:

fun readFileText(fileName: String): String {
    return assets.open(fileName).bufferedReader().use { it.readText() }
}

7

getAssets() เมธอดจะทำงานเมื่อคุณโทรภายในคลาสกิจกรรม

หากคุณเรียกใช้เมธอดนี้ในคลาสที่ไม่ใช่กิจกรรมคุณต้องเรียกใช้เมธอดนี้จากบริบทซึ่งถูกส่งจากคลาสกิจกรรม ดังนั้นด้านล่างเป็นบรรทัดโดยคุณสามารถเข้าถึงวิธีการ

ContextInstance.getAssets();

ContextInstance อาจถูกส่งผ่านเป็นคลาสกิจกรรมนี้


5

การอ่านและการเขียนไฟล์นั้นเป็นเรื่องที่ละเอียดและเป็นไปได้ง่าย หลีกเลี่ยงคำตอบเหล่านี้และใช้Okioแทน:

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

1
คุณรู้ไหมว่าทำไมสิ่งนี้ถึงดูสวยงามและสั้นกว่านี้? ก็อย่างน้อยครึ่งหนึ่งของโค้ดที่นี่ ส่วนที่ละเว้น: 1) IOExceptionลอง / จับบล็อก 2) ปิดสตรีมในกรณียกเว้นจะถูกโยน 3) รหัสนี้อ่านบรรทัดเดียวไม่ใช่ไฟล์ทั้งหมด ประสิทธิภาพการทำงานห้องสมุดนี้เป็นหนึ่งในประเภทที่แน่นอนไม่ต้องสงสัยเลยว่า ตอนนี้บอกฉันฉันควรหลีกเลี่ยง "คำตอบเหล่านี้" และใช้ Okio เพื่ออ่านไฟล์หรือไม่ คำตอบคือไม่ยกเว้นว่ามันเป็นส่วนหนึ่งของแอพของคุณอยู่แล้ว
Farid

อัปเดตคำตอบของฉัน
Saket

4

นี่คือวิธีการอ่านไฟล์ในเนื้อหา:

/**
 * Reads the text of an asset. Should not be run on the UI thread.
 * 
 * @param mgr
 *            The {@link AssetManager} obtained via {@link Context#getAssets()}
 * @param path
 *            The path to the asset.
 * @return The plain text of the asset
 */
public static String readAsset(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;
}

นี่เป็นคำตอบที่ดี แต่เป็นวิธีการที่ไม่ดีในการใช้การต่อข้อมูลสตริง พิจารณาใช้ StringBuilder แทน StringBuilder contentBuilder = ใหม่ StringBuilder (); ในขณะที่ ((บรรทัด = reader.readLine ())! = null) {builder.append ("\ n") ผนวก (บรรทัด); } และในตอนท้ายคุณสามารถสร้างวัตถุ String ใหม่โดยสิ่งนี้: content = contentBuilder.toString ();
Barterio

4

คุณสามารถโหลดเนื้อหาจากไฟล์ พิจารณาว่าไฟล์นั้นมีอยู่ในโฟลเดอร์ทรัพย์สิน

public static InputStream loadInputStreamFromAssetFile(Context context, String fileName){
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(fileName);
        return is;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static String loadContentFromFile(Context context, String path){
    String content = null;
    try {
        InputStream is = loadInputStreamFromAssetFile(context, path);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        content = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return content;
}

ตอนนี้คุณสามารถรับเนื้อหาได้โดยการเรียกใช้ฟังก์ชันดังต่อไปนี้

String json= FileUtil.loadContentFromFile(context, "data.json");

การพิจารณา data.json ถูกเก็บไว้ที่ Application \ app \ src \ main \ assets \ data.json


3

ใน MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tvView = (TextView) findViewById(R.id.tvView);

        AssetsReader assetsReader = new AssetsReader(this);
        if(assetsReader.getTxtFile(your_file_title)) != null)
        {
            tvView.setText(assetsReader.getTxtFile(your_file_title)));
        }
    }

นอกจากนี้คุณยังสามารถสร้างคลาสแยกต่างหากที่ทำงานได้ทั้งหมด

public class AssetsReader implements Readable{

    private static final String TAG = "AssetsReader";


    private AssetManager mAssetManager;
    private Activity mActivity;

    public AssetsReader(Activity activity) {
        this.mActivity = activity;
        mAssetManager = mActivity.getAssets();
    }

    @Override
    public String getTxtFile(String fileName)
    {
        BufferedReader reader = null;
        InputStream inputStream = null;
        StringBuilder builder = new StringBuilder();

        try{
            inputStream = mAssetManager.open(fileName);
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;

            while((line = reader.readLine()) != null)
            {
                Log.i(TAG, line);
                builder.append(line);
                builder.append("\n");
            }
        } catch (IOException ioe){
            ioe.printStackTrace();
        } finally {

            if(inputStream != null)
            {
                try {
                    inputStream.close();
                } catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }

            if(reader != null)
            {
                try {
                    reader.close();
                } catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        }
        Log.i(TAG, "builder.toString(): " + builder.toString());
        return builder.toString();
    }
}

ในความคิดของฉันดีกว่าที่จะสร้างอินเทอร์เฟซ แต่มันไม่จำเป็น

public interface Readable {
    /**
     * Reads txt file from assets
     * @param fileName
     * @return string
     */
    String getTxtFile(String fileName);
}

2

หากคุณใช้คลาสอื่นนอกเหนือจากกิจกรรมคุณอาจต้องการทำเช่นนั้น

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( YourApplication.getInstance().getAssets().open("text.txt"), "UTF-8"));

2

ใช้ Kotlin คุณสามารถทำสิ่งต่อไปนี้เพื่ออ่านไฟล์จากเนื้อหาใน 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())
}

2

มันอาจจะสายเกินไป แต่เพื่อประโยชน์ของคนอื่นที่มองหาคำตอบพีช:

public static String loadAssetFile(Context context, String fileName) {
    try {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
        StringBuilder out= new StringBuilder();
        String eachline = bufferedReader.readLine();
        while (eachline != null) {
            out.append(eachline);
            eachline = bufferedReader.readLine();
        }
        return out.toString();
    } catch (IOException e) {
        Log.e("Load Asset File",e.toString());
    }
    return null;
}

1

cityfile.txt

   public void getCityStateFromLocal() {
        AssetManager am = getAssets();
        InputStream inputStream = null;
        try {
            inputStream = am.open("city_state.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String[]> map = new HashMap<String, String[]>();
        try {
            map = mapper.readValue(getStringFromInputStream(inputStream), new TypeReference<Map<String, String[]>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        ConstantValues.arrayListStateName.clear();
        ConstantValues.arrayListCityByState.clear();
        if (map.size() > 0)
        {
            for (Map.Entry<String, String[]> e : map.entrySet()) {
                CityByState cityByState = new CityByState();
                String key = e.getKey();
                String[] value = e.getValue();
                ArrayList<String> s = new ArrayList<String>(Arrays.asList(value));
                ConstantValues.arrayListStateName.add(key);
                s.add(0,"Select City");
                cityByState.addValue(s);
                ConstantValues.arrayListCityByState.add(cityByState);
            }
        }
        ConstantValues.arrayListStateName.add(0,"Select States");
    }
 // Convert InputStream to String
    public String getStringFromInputStream(InputStream is) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb + "";

    }

ลิงค์จะกลับมาตอนนี้File no longer available That file has now been permanently removed and cannot be recovered
gregn3

1

คลาสสแกนเนอร์อาจทำให้สิ่งนี้ง่ายขึ้น

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

คุณสามารถรวม 2 บรรทัด sb.append (scanner.nextLine ()); sb.append ( '\ n'); เพื่อ sb.appendln (scanner.nextLine ());
Mojtaba

0

@HpTermตอบรุ่น Kotlin:

private fun getDataFromAssets(): String? {

    var bufferedReader: BufferedReader? = null
    var data: String? = null

    try {
        bufferedReader = BufferedReader(
            InputStreamReader(
                activity?.assets?.open("Your_FILE.html"),     
                "UTF-8"
            )
        )                  //use assets? directly if in activity

       var mLine:String = bufferedReader?.readLine()
        while (mLine != null) {
            data+= mLine
            mLine=bufferedReader.readLine()
        }

    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        try {
            bufferedReader?.close()
        } catch (e: Exception) {
           e.printStackTrace()
        }
    }
    return data
}

ขณะนี้สิ่งนี้จะเติม null ให้กับสตริง การเปลี่ยนแปลงที่จำเป็น: var mLine:Stringควรจะvar mLine:String? var data: String?เป็นvar data = ""ประเภทผลตอบแทนควรเป็นString
fupduck

0

นี่คือวิธีการที่จะได้รับการประเมินโดยInputStreamไฟล์ในที่assetsโฟลเดอร์โดยไม่ต้องContext, Activity, หรือFragment Applicationวิธีรับข้อมูลจากคุณInputStreamนั้นขึ้นอยู่กับคุณ มีคำแนะนำมากมายสำหรับคำตอบอื่น ๆ ที่นี่

Kotlin

val inputStream = ClassLoader::class.java.classLoader?.getResourceAsStream("assets/your_file.ext")

ชวา

InputStream inputStream = ClassLoader.class.getClassLoader().getResourceAsStream("assets/your_file.ext");

การเดิมพันทั้งหมดจะปิดถ้ามีการClassLoaderเล่นแบบกำหนดเอง

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.