1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | String res = ""; try{ //得到资源中的Raw数据流 InputStream in = getResources().openRawResource(R.raw.test); //得到数据的大小 int length = in.available(); byte [] buffer = new byte[length]; //读取数据 in.read(buffer); //依test.txt的编码类型选择合适的编码,如果不调整会乱码 res = EncodingUtils.getString(buffer, "BIG5"); //关闭 in.close(); }catch(Exception e){ e.printStackTrace(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | String fileName = "test.txt"; //文件名字 String res=""; try{ //得到资源中的asset数据流 InputStream in = getResources().getAssets().open(fileName); int length = in.available(); byte [] buffer = new byte[length]; in.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); }catch(Exception e){ e.printStackTrace(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //写数据 public void writeFile(String fileName,String writestr{ try{ FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); byte [] bytes = writestr.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } } //读数据 public String readFile(String fileName){ String res=""; try{ FileInputStream fin = openFileInput(fileName); int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); } catch(Exception e){ e.printStackTrace(); } return res; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //写数据到SD中的文件 public void writeFileSdcardFile(String fileName,String write_str){ try{ FileOutputStream fout = new FileOutputStream(fileName); byte [] bytes = write_str.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } } //读SD中的文件 public String readFileSdcardFile(String fileName){ String res=""; try{ FileInputStream fin = new FileInputStream(fileName); int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); } catch(Exception e){ e.printStackTrace(); } return res; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //读文件 public String readSDFile(String fileName) { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte [] buffer = new byte[length]; fis.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fis.close(); return res; } //写文件 public void writeSDFile(String fileName, String write_str){ File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); byte [] bytes = write_str.getBytes(); fos.write(bytes); fos.close(); } |
1 2 3 4 5 6 7 8 9 10 | String Name = File.getName(); //获得文件或文件夹的名称: String parentPath = File.getParent(); 获得文件或文件夹的父目录 String path = File.getAbsoultePath();//绝对路经 String path = File.getPath();//相对路经 File.createNewFile();//建立文件 File.mkDir(); //建立文件夹 File.isDirectory(); //判断是文件或文件夹 File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名 File.renameTo(dest); //修改文件夹和文件名 File.delete(); //删除文件夹或文件 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /* * 程序功能:演示了RandomAccessFile类的操作,同时实现了一个文件复制操作。 */ package com.lwj.demo; import java.io.*; public class RandomAccessFileDemo { public static void main(String[] args) throws Exception { RandomAccessFile file = new RandomAccessFile("file", "rw"); // 以下向file文件中写数据 file.writeInt(20);// 占4个字节 file.writeDouble(8.236598);// 占8个字节 file.writeUTF("这是一个UTF字符串");// 这个长度写在当前文件指针的前两个字节处,可用readShort()读取 file.writeBoolean(true);// 占1个字节 file.writeShort(395);// 占2个字节 file.writeLong(2325451l);// 占8个字节 file.writeUTF("又是一个UTF字符串"); file.writeFloat(35.5f);// 占4个字节 file.writeChar('a');// 占2个字节 file.seek(0);// 把文件指针位置设置到文件起始处 // 以下从file文件中读数据,要注意文件指针的位置 System.out.println("——————从file文件指定位置读数据——————"); System.out.println(file.readInt()); System.out.println(file.readDouble()); System.out.println(file.readUTF()); file.skipBytes(3);// 将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。 System.out.println(file.readLong()); file.skipBytes(file.readShort()); // 跳过文件中“又是一个UTF字符串”所占字节,注意readShort()方法会移动文件指针,所以不用加2。 System.out.println(file.readFloat()); //以下演示文件复制操作 System.out.println("——————文件复制(从file到fileCopy)——————"); file.seek(0); RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw"); int len=(int)file.length();//取得文件长度(字节数) byte[] b=new byte[len]; file.readFully(b); fileCopy.write(b); System.out.println("复制完成!"); } } |
欢迎光临 德克云技术联盟 (http://www.decoclouds.com/) |