该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
jdk用的是1.8,想知道为什么会报错
报错信息:Variable resource not allowed here for source level below 9
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 1、封装拷贝
* 2、封装释放
* @author 14988
*
*/
public class FileUtils2 {
public static void main(String[] args) {
/*
try {
InputStream is = new FileInputStream("tp.jpg");
OutputStream os = new FileOutputStream("tp_copy2.jpg");
copy(is,os);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
byte datas[] = null;
try {
InputStream is = new FileInputStream("tp.jpg");
ByteArrayOutputStream os = new ByteArrayOutputStream();
copy(is,os);
datas = os.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ByteArrayInputStream is = new ByteArrayInputStream(datas);
OutputStream os = new FileOutputStream("tp_copy2.jpg");
copy(is,os);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void copy(InputStream is,OutputStream os) {
try(is;os) {
byte flush[] = new byte[1024];
int len = -1;
while((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}