Android 对文件字节的读和写方法超详细方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        private static final String CAMERA_DATA =                 
            "/sys/bus/platform/drivers/battery/battery/cam_front2";//要读写的文件节点

        readFile(CAMERA_DATA);//读取节点的值

        writeData(CAMERA_DATA ,"1");//这里往节点写入string类型的1
        
    }

//往节点写值,pach为要写的文件节点,value为要写入的值
private void writeData(String path, String value) {
		try {
			BufferedWriter bufWriter = null;
			bufWriter = new BufferedWriter(new FileWriter(path));
			bufWriter.write(value + "");
			bufWriter.close();
			Log.d(TAG, "write writeData: " + value);
		} catch (IOException e) {
			Log.e(TAG,"erro= "+ Log.getStackTraceString(e));
		}
	}
	

	private static String readFile(String filename) {    
		File file = new File(filename);    
		if (!file.exists()) {       
			return "";    
		}    
		FileReader fr = null;    
		BufferedReader br = null;    
		String str = null;    
		try {        
			fr = new FileReader(file);        
			br = new BufferedReader(fr);        
			str = br.readLine();        
			if (str == null) {            
				str = "";        
			}    
		} catch (IOException e) {        
			e.printStackTrace();    
		} finally {        
			try {            
				if (fr != null)                
					fr.close();            
				if (br != null)                
					br.close();        
			} catch (IOException e) {            
				e.printStackTrace();        
				}    
		}    
		return str;
	}

 

发布了7 篇原创文章 · 获赞 0 · 访问量 285
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览