android向节点写入或者读取值

1.向节点写入值:

// 写文件
	private int fileWrite(String fileName, char[] buffer) {
		int i = -1;
		try {
			File file = new File(fileName);
			if(!file.exists()){
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(fileName);
			fileWriter.write(buffer);
			fileWriter.close();
			i = 0;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return i;
	}

2.从节点读取值:

// 文件读取
	private int fileRead(String fileName, char[] buffer) {
		int line = -1;
		FileReader fileReader;
		try {
			fileReader = new FileReader(fileName);
			line = fileReader.read(buffer);
			fileReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;
	}
public static String readFile(String fileName){
        StringBuilder sb = new StringBuilder("");
        try {
            File file = new File(fileName);
            //打开文件输入流
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len = inputStream.read(buffer);
            //读取文件内容
            while (len > 0) {
                sb.append(new String(buffer, 0, len));
                //继续将数据放到buffer中
                len = inputStream.read(buffer);
            }
            //关闭输入流
            inputStream.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return sb.toString();
    }

你可能感兴趣的:(技术文档)