java(android): 尝试压缩 I/O 并失败
基本上我有一个相当长的文本,我需要将其压缩,将其存储在文件中,并稍后读出。这就是我所做的。
public static void outCompressNIO(String outFile, String src) {
FileChannel fc = null;
GZIPOutputStream gz = null;
try {
fc = new FileOutputStream(outFile).getChannel();
gz = new GZIPOutputStream(Channels.newOutputStream(fc));
gz.write(src.getBytes());
gz.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
gz.close();
fc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void inCompressNIO(String inFile) {
FileChannel fc = null;
GZIPInputStream gz = null;
try {
fc = new FileInputStream(inFile).getChannel();
gz = new GZIPInputStream(Channels.newInputStream(fc));
byte fileContent[] = new byte[1024];
StringBuilder sb = new StringBuilder();
while ((gz.read(fileContent)) != -1) {
sb.append(new String(fileContent));
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
gz.close();
fc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是我从压缩文件中返回的字符串与原始的字符串各地都不同。有人知道为什么吗?顺便说一句,我的主要目标是我需要有效地将一个长字符串(大约15M)压缩到一个文件中,并将其解压缩到android上的主内存中。我尝试在java中使用Object IO来实现这一点,在我的电脑上实现没有问题。但在android上,如果字符串对象超过3M,则需要很长时间才能将我从ObjectInputStream获得的对象转换为String。所以我正在尝试其他方法来避免铸造。以上是我的尝试之一,但由于我对 NIO 知之甚少,我可能做错了,所以任何人都知道更有效的方法来实现这一点,请为我指出正确的方向,非常感谢。
Basically I have a text that is quite long, I need to compress it, store it in a file, and read it out later. here is what I did.
public static void outCompressNIO(String outFile, String src) {
FileChannel fc = null;
GZIPOutputStream gz = null;
try {
fc = new FileOutputStream(outFile).getChannel();
gz = new GZIPOutputStream(Channels.newOutputStream(fc));
gz.write(src.getBytes());
gz.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
gz.close();
fc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void inCompressNIO(String inFile) {
FileChannel fc = null;
GZIPInputStream gz = null;
try {
fc = new FileInputStream(inFile).getChannel();
gz = new GZIPInputStream(Channels.newInputStream(fc));
byte fileContent[] = new byte[1024];
StringBuilder sb = new StringBuilder();
while ((gz.read(fileContent)) != -1) {
sb.append(new String(fileContent));
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
gz.close();
fc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
But the string I got back from the compressed file is different with the original one from place to place. Anyone knows why? By the way, my main goal is that I need to effectively compress a long string(around 15M) into a file, and decompress it into main memory on android. I tried to just use Object IO in java to achieve this, it is achieved with no problem on my pc. But on android, if the string object is more than 3M, it takes forever to cast the object I got from ObjectInputStream to String. So I am trying other ways to avoid casting. The above is one of my attempts, but since I know little about NIO, I am probably doing it all wrong, so anyone knows a more efficient way to achieve this, please point me to the right direction, Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
追加到 sb 时使用该变量,即 sb.append(new String(buffer,0,count, charset));
Use that variable when appending to sb, i.e. sb.append(new String(buffer,0,count, charset));