| | |
| | | import android.app.Application; |
| | | import android.content.Context; |
| | | import android.content.res.Resources; |
| | | import android.os.Environment; |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.exam.lib.MyLog; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | |
| | | import java.io.OutputStream; |
| | | |
| | | public class FileUtil { |
| | | private static final String TAG = "FileUtil"; |
| | | /** |
| | | * 读取assert目录下 txt文本文件内容 |
| | | * @param context |
| | |
| | | return stringBuffer; |
| | | } |
| | | |
| | | public static void createdirs(Context context){ |
| | | String dir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+context.getPackageName(); |
| | | |
| | | File file = new File(dir); |
| | | if (!file.exists()){ |
| | | file.mkdir(); |
| | | }else{ |
| | | Log.i(TAG, "createdirs: 目录已经存在"); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static StringBuffer readTxtFileFromSD(Context context,String fileName){ |
| | | String lineTxt = null; |
| | | StringBuffer stringBuffer = new StringBuffer(); |
| | | try { |
| | | InputStream inputStream = null; |
| | | String dir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+context.getPackageName(); |
| | | File file = new File(dir,fileName); |
| | | if (!file.exists()){ |
| | | MyLog.d(TAG,fileName+"文件不存在"); |
| | | return null; |
| | | } |
| | | try { |
| | | inputStream = new FileInputStream(file); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | InputStreamReader inputStreamReader = new InputStreamReader(inputStream); |
| | | BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
| | | while((lineTxt = bufferedReader.readLine()) != null){ |
| | | System.out.println(lineTxt); |
| | | stringBuffer.append(lineTxt); |
| | | |
| | | } |
| | | inputStreamReader.close(); |
| | | bufferedReader.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return stringBuffer; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param fromFile 源文件路径包括文件名(绝对路径) |