📄 readerfile.java
字号:
package com.cnxinshe.noteview;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
public class ReaderFile {
public byte[] readFile(String testFileName) {
byte data[] = null;
try {
FileConnection fc = (FileConnection) Connector.open(testFileName);
DataInputStream dis = fc.openDataInputStream();
/** ********************************************************** */
int ch;// 每次读出的数据
int index = 0;// 读取的数据的总索引
int len = 1024;// 放数据的空间不够时,每次扩充空间的大小为1024字节
byte buf[];// 暂时存放从data[]拷贝出来的数据
data = new byte[len];// 先初步设定一个1k的内存空间
while ((ch = dis.read()) != -1) {
data[index] = (byte) ch;
index++;
if (index >= len) {
len += 1024;
buf = new byte[len];
System.arraycopy(data, 0, buf, 0, index);
data = null;
data = buf;
}
}
// 此时data[]的长度可能要比实际数据多,最后的一些字节可能是一些无效的数据,去掉无效数据
if ((index % 1024) == 0) {
buf = new byte[index];// index的值是数据的实际大小
System.arraycopy(data, 0, buf, 0, index);
data = null;
data = buf;
}
/** ********************************************************** */
if (dis != null) {
dis.close();
dis = null;
}
if (fc != null) {
fc.close();
fc = null;
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public String myReadLine(String testFileName) {
// 函数头定义了函数的参数为文件名组成的字符串,返回值为一个字符串。
InputStream in = this.getClass().getResourceAsStream(testFileName);
// 由文件名参数来定义一个输入流对象变量in
ByteArrayOutputStream s;
s = new ByteArrayOutputStream(); // 产生内存数组输出字节流对象变量S
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
int ch = 0;
ch = in.read(); // 由输入流对象变量in的read方法以字节为单位来读取文件的内容,读取到文件尾时的值为-1。
while (ch != -1) {
// 如果未读到文件尾把读取的内容写入S变量中,并读取下一字节内容
s.write(ch);
ch = in.read();
}
in.close(); // 关闭输入流对象。
} catch (IOException ioe) {
System.out.println(ioe.toString());
}
String str = s.toString(); // 将S变量转为字符串
try {
s.close(); // 关闭输出流对象
} catch (IOException ioe) {
System.out.println(ioe.toString());
}
return str.trim(); // 返回文件中的内容字符串
}
public String read_UTF(String name) {
String strReturn = ""; InputStream in = null; byte[] word_utf = new byte[150000];
try { in = getClass().getResourceAsStream(name);
in.read(word_utf); in.close();
strReturn = new String(word_utf ,"UTF-8"); } catch (Exception e) {
System.out.println("readUTF Error." + e.toString()); } finally { in = null; } return strReturn; } private String read_Uni(String resource) { byte word_uni[] = new byte[1024];
String strReturn = ""; InputStream is;
try { is = getClass().getResourceAsStream(resource);
is.read(word_uni); is.close(); StringBuffer stringbuffer = new StringBuffer("");
for (int j = 0; j < word_uni.length; ) { int k = word_uni[j++];
//注意在这个地方进行了码制的转换
if (k < 0) { k += 256; } int l = word_uni[j++];
if (l < 0) { l += 256; } char c = (char) (k + (l << 8));
//把高位和低位数组装起来
stringbuffer.append(c); }
strReturn = stringbuffer.toString();
} catch (IOException e) {
e.printStackTrace(); } finally { is = null; }
return strReturn; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -