RandomAccessFile 的默认编码为ISO-8859-1

private String getEncodeType(RandomAccessFile randomFile) {
		String encodeType = "utf-8";
		
		if(randomFile == null) {
			return encodeType;
		}
		
		try {
			randomFile.seek(1l);
			
			byte[] first3bytes = new byte[3];
			randomFile.read(first3bytes);
			
			if(first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
                    && first3bytes[2] == (byte) 0xBF) {
				encodeType = "utf-8";
			}
			else if(first3bytes[0] == (byte) 0xFF
                    && first3bytes[1] == (byte) 0xFE) {
				encodeType = "unicode";
			}
			 else if (first3bytes[0] == (byte) 0xFE
                     && first3bytes[1] == (byte) 0xFF) {
				 encodeType = "utf-16be";
			 }
			 else if (first3bytes[0] == (byte) 0xFF
                     && first3bytes[1] == (byte) 0xFF) {
				 encodeType = "utf-16le";
			 }
			 else {
                 encodeType = "gbk";
			 }
			
			randomFile.seek(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return encodeType;
	}

RandomAccessFile 的默认编码为ISO-8859-1,出现中文乱码需要如此

readStr.getBytes("ISO-8859-1"), encode);

encode为你所读文件的编码格式,获取方法

有错请指正



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