Java是';不要删除文件
Java没有删除文件。我确保它有权限(对所有人都有完全控制权,对Unix用户有777权限)。System.out.println(currchive.delete())总是返回false。有人知道为什么吗?我尝试用try-catch安全异常来包装它,但没有抛出。帮忙?我确保我所有的溪流都被关闭了,但它仍然返回错误 这是我的密码: targetzipmanagerview.java的addFile方法Java是';不要删除文件,java,file,delete-file,Java,File,Delete File,Java没有删除文件。我确保它有权限(对所有人都有完全控制权,对Unix用户有777权限)。System.out.println(currchive.delete())总是返回false。有人知道为什么吗?我尝试用try-catch安全异常来包装它,但没有抛出。帮忙?我确保我所有的溪流都被关闭了,但它仍然返回错误 这是我的密码: targetzipmanagerview.java的addFile方法 private void addInside(java.awt.event.ActionEven
private void addInside(java.awt.event.ActionEvent evt) {
if (isCurrArchived == EXTRACTING){
final ActionEvent acevt = evt;
JFileChooser jfc = new JFileChooser();
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
jfc.setMultiSelectionEnabled(true);
jfc.setAcceptAllFileFilterUsed(true);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.setDialogTitle("Choose files to add:");
int result = jfc.showOpenDialog(theframe);
if (result == JFileChooser.APPROVE_OPTION){
File[] addsf = jfc.getSelectedFiles();
totalNewFilesToAdd = new ArrayList<File>();
ArrayList<File> newFilesToAdd = new ArrayList<File>();
ArrayList<AbsRelFile> dirEntries = new ArrayList<AbsRelFile>();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(currArchive));
} catch (FileNotFoundException fnfe){
return;
}
currAStream = null;
currCStream = null;
CompressorStreamFactory cFactory = new CompressorStreamFactory();
try {
CompressorInputStream temp = cFactory.createCompressorInputStream(in);
currCStream = temp.getName();
in = new BufferedInputStream(temp);
} catch (CompressorException ex) {
//if it isn't compressed, do nothing!
}
ArchiveStreamFactory aFactory = new ArchiveStreamFactory();
realIn = null;
useZipFile = false;
try {
realIn = aFactory.createArchiveInputStream(in);
currAStream = realIn.getArchiveName();
if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
useZipFile = true;
try {
realIn.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
} catch (ArchiveException ae){
//Bad Archive! oops!
new ErrorDialog(theframe, false).setVisible(true);
try {
in.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
return;
}
bytesToWrite = 0;
if (!useZipFile){
ArchiveEntry ae;
try {
while ((ae = realIn.getNextEntry()) != null){
bytesToWrite += ae.getSize();
}
} catch (IOException ioe){
ioe.printStackTrace();
}
try {
realIn.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
} else {
ZipArchiveEntry ae;
try {
ZipFile zf = new ZipFile(currArchive);
Enumeration en = zf.getEntriesEnum();
while (en.hasMoreElements()){
ae = (ZipArchiveEntry) en.nextElement();
bytesToWrite += ae.getSize();
}
zf.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
try {
in = new BufferedInputStream(new FileInputStream(currArchive));
} catch (FileNotFoundException fnfe){
return;
}
currAStream = null;
currCStream = null;
cFactory = new CompressorStreamFactory();
try {
CompressorInputStream temp = cFactory.createCompressorInputStream(in);
currCStream = temp.getName();
in = new BufferedInputStream(temp);
} catch (CompressorException ex) {
//if it isn't compressed, do nothing!
}
aFactory = new ArchiveStreamFactory();
realIn = null;
useZipFile = false;
try {
realIn = aFactory.createArchiveInputStream(in);
currAStream = realIn.getArchiveName();
if (currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
useZipFile = true;
try {
realIn.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
} catch (ArchiveException ae){
//Bad Archive! oops!
new ErrorDialog(theframe, false).setVisible(true);
try {
in.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
return;
}
for (int i = 0;i<addsf.length;i++){
if (addsf[i].isFile()){
newFilesToAdd.add(addsf[i]);
totalNewFilesToAdd.add(addsf[i]);
}
if (addsf[i].isDirectory()){
dirEntries.addAll(recurseDir(addsf[i]));
totalNewFilesToAdd.addAll(recurseDir(addsf[i]));
}
}
for (int i = 0;i<totalNewFilesToAdd.size();i++){
bytesToWrite += totalNewFilesToAdd.get(i).length();
}
aentries = new ArchiveEntry[totalNewFilesToAdd.size()];
for (int i = 0;i<newFilesToAdd.size();i++){
try {
aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, newFilesToAdd.get(i), newFilesToAdd.get(i).getName());
} catch (ArchiveException ae){
ae.printStackTrace();
}
}
for (int i = newFilesToAdd.size();i<totalNewFilesToAdd.size();i++){
try {
aentries[i] = new ArchiveStreamFactory().createArchiveEntry(currAStream, dirEntries.get(i-newFilesToAdd.size()), dirEntries.get(i-newFilesToAdd.size()).getRelativePath());
} catch (ArchiveException ae){
ae.printStackTrace();
}
}
Thread iothread = new Thread(new Runnable(){
public void run(){
if (!useZipFile){
try {
EventQueue.invokeLater(new Runnable(){
public void run(){
addingBox = new AddingBox(theframe, false);
}
});
File outfile = File.createTempFile("ScruffArchiveManager", null);
ArchiveOutputStream tempout = null;
CompressorOutputStream tempcomp = null;
if (currCStream == null) tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream ,new BufferedOutputStream(new FileOutputStream(outfile)));
else {
tempcomp = new CompressorStreamFactory().createCompressorOutputStream(currCStream, new BufferedOutputStream(new FileOutputStream(outfile)));
tempout = new ArchiveStreamFactory().createArchiveOutputStream(currAStream, tempcomp);
}
if (currAStream.equals(ArchiveStreamFactory.TAR)){
TarArchiveOutputStream t = (TarArchiveOutputStream)tempout;
t.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
tempout = t;
}
ArchiveEntry ae;
long bytesWritten = 0;
while ((ae = realIn.getNextEntry()) != null){
long semiBytesWritten = 0;
tempout.putArchiveEntry(ae);
byte[] buffer = new byte[8024];
int n = 0;
if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
while (semiBytesWritten < ae.getSize() && -1 != (n = realIn.read(buffer))){
tempout.write(buffer, 0, n);
semiBytesWritten += n;
bytesWritten += n;
final long k = bytesWritten;
EventQueue.invokeLater(new Runnable(){
public void run(){
addingBox.progressListener(new Progress(k, bytesToWrite));
}
});
if (ae.getSize() < semiBytesWritten + 8024) buffer = new byte[(int)(ae.getSize()-semiBytesWritten)];
}
tempout.closeArchiveEntry();
}
realIn.close();
for (int i = 0;i<aentries.length;i++){
InputStream in = new BufferedInputStream(new FileInputStream(aentries[i].getFile()));
tempout.putArchiveEntry(aentries[i]);
int n = 0;
byte[] buffer = new byte[8024];
while (-1 != (n = in.read(buffer))){
tempout.write(buffer, 0, n);
bytesWritten += n;
final long k = bytesWritten;
EventQueue.invokeLater(new Runnable(){
public void run(){
addingBox.progressListener(new Progress(k, bytesToWrite));
}
});
}
in.close();
tempout.closeArchiveEntry();
}
tempout.close();
final File ne = new File(currArchive.getAbsolutePath());
System.out.println(currArchive.delete());
outfile.renameTo(ne);
EventQueue.invokeLater(new Runnable(){
public void run(){
closeArchive(acevt);
openArchive(ne);
addingBox.progressListener(new Progress(bytesToWrite, bytesToWrite));
}
});
} catch (CompressorException ce){
ce.printStackTrace();
} catch (ArchiveException ae){
ae.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
});
iothread.start();
}
}
}
private void addInside(java.awt.event.ActionEvent evt){
if(iscurrchived==正在提取){
最终行动事件acevt=evt;
JFileChooser jfc=新的JFileChooser();
setDialogType(JFileChooser.OPEN_对话框);
jfc.setMultiSelectionEnabled(真);
jfc.setAcceptalFileFilterUsed(true);
setFileSelectionMode(JFileChooser.FILES_和_目录);
setDialogTitle(“选择要添加的文件:”);
int result=jfc.showOpenDialog(框架);
if(result==JFileChooser.APPROVE\u选项){
文件[]addsf=jfc.getSelectedFiles();
totalNewFilesToAdd=newArrayList();
ArrayList newFilesToAdd=新ArrayList();
ArrayList dirEntries=新的ArrayList();
InputStream in=null;
试一试{
in=新的BufferedInputStream(新的FileInputStream(currArchive));
}捕获(FileNotFoundException fnfe){
返回;
}
currAStream=null;
currCStream=null;
CompressorStreamFactory CFFactory=新的CompressorStreamFactory();
试一试{
CompressorInputStream temp=cFactory.createCompressorInputStream(in);
currCStream=temp.getName();
in=新的BufferedInputStream(温度);
}捕捉(压缩机例外){
//如果它没有被压缩,什么也不要做!
}
ArchiveStreamFactory AfFactory=新的ArchiveStreamFactory();
realIn=null;
useZipFile=false;
试一试{
realIn=aFactory.createArchiveInputStream(in);
currAStream=realIn.getArchiveName();
if(currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
useZipFile=true;
试一试{
realIn.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
}捕获(归档例外ae){
//糟糕的档案!哎呀!
新建ErrorDialog(帧,false).setVisible(true);
试一试{
in.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
返回;
}
bytesToWrite=0;
如果(!useZipFile){
档案馆;
试一试{
而((ae=realIn.getNextEntry())!=null){
bytesToWrite+=ae.getSize();
}
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
试一试{
realIn.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}否则{
紫杉属;
试一试{
ZipFile zf=新ZipFile(当前存档);
枚举en=zf.getEntriesNum();
while(en.hasMoreElements()){
ae=(ZipArchiveEntry)en.nextElement();
bytesToWrite+=ae.getSize();
}
zf.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
试一试{
in=新的BufferedInputStream(新的FileInputStream(currArchive));
}捕获(FileNotFoundException fnfe){
返回;
}
currAStream=null;
currCStream=null;
cFactory=新压缩机treamfactory();
试一试{
CompressorInputStream temp=cFactory.createCompressorInputStream(in);
currCStream=temp.getName();
in=新的BufferedInputStream(温度);
}捕捉(压缩机例外){
//如果它没有被压缩,什么也不要做!
}
aFactory=新ArchiveStreamFactory();
realIn=null;
useZipFile=false;
试一试{
realIn=aFactory.createArchiveInputStream(in);
currAStream=realIn.getArchiveName();
if(currAStream.equalsIgnoreCase(ArchiveStreamFactory.ZIP)){
useZipFile=true;
试一试{
realIn.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
}捕获(归档例外ae){
//糟糕的档案!哎呀!
新建ErrorDialog(帧,false).setVisible(true);
试一试{
in.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
返回;
}
对于(int i=0;i尝试myFile.toPath().delete()
以获取错误。并且永远不要忘记关闭FileInputStream
,喜欢java是多么简洁;)您可能想将其分解一些。发布SSCCE()此代码无法读取。没有像toPath.toString和getPath这样的方法都返回字符串,它们没有delete()方法。toPath()
是java.nio2,它只是java 7。