?? java文件轉化.txt
字號:
// 整數到字節數組轉換
public static byte[] int2bytes(int n) {
byte[] ab = new byte[4];
ab[0] = (byte) (0xff & n);
ab[1] = (byte) ((0xff00 & n) >> 8);
ab[2] = (byte) ((0xff0000 & n) >> 16);
ab[3] = (byte) ((0xff000000 & n) >> 24);
return ab;
}
// 字節數組到整數的轉換
public static int bytes2int(byte b[]) {
int s = 0;
s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8
| (b[3] & 0xff);
return s;
}
// 字節轉換到字符
public static char byte2char(byte b) {
return (char) b;
}
private final static byte[] hex = "0123456789ABCDEF".getBytes();
private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
// 從字節數組到十六進制字符串轉換
public static String Bytes2HexString(byte[] b) {
byte[] buff = new byte[2 * b.length];
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
// 從十六進制字符串到字節數組轉換
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}
把文件生成byte 字節數組
FileInputStream fileForInput = new FileInputStream("d:/test.jpg");
String content = new String();
byte[] bytes=new byte[fileForInput.available()]; fileForInput.read(bytes); content=new sun.misc.BASE64Encoder().encode(bytes); //具體的編碼方法
fileForInput.close();
把字節數組轉成文件
package com.lizhe;
import java.io.*;
import java.sql.*;
class GetImg {
private File file = null;
public void blobRead(String outfile, int picID) throws Exception {
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select img from imgt where id=?");
pstmt.setInt(1, picID); // 傳入要取的圖片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,則創建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("img");
int size = 0;
while ((size = is.read(Buffer)) != -1) {
// System.out.println(size);
fos.write(Buffer, 0, size);
}
} catch (Exception e) {
System.out.println( e.getMessage());
} finally {
// 關閉用到的資源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
GRANT SELECT,INSERT,UPDATE,DELETE,FILE ON goodemail.* TO wj@% identified by ''
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -