给定以下代码,我使用 gunzip 压缩“.gz”文件:
public class TestCSVDataToMap {
private static final String INPUT_GZIP_FILE = "/opt/old.csv.gz";
private static final String OUTPUT_FILE = "/opt/new.csv";
public static void main(String[] args) throws IOException{
TestCSVDataToMap dm = new TestCSVDataToMap();
dm.gunzipIt();
}
public void gunzipIt(){
byte[] buffer = new byte[1024];
try{
GZIPInputStream gzis =
new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));
FileOutputStream out =
new FileOutputStream(OUTPUT_FILE);
int len;
while ((len = gzis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
gzis.close();
out.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
抛出这样的异常:
java.io.FileNotFoundException: /opt/new.csv (Permission denied)
这是因为路径 '/opt' 不允许进行任何更改。但我可以使用
sudo gunzip /opt/old.csv.gz
解压缩文件。那么在 Java 中,我应该怎么做才能表现得像“sudo”?