例如,我想压缩存储在/Users/me/Desktop/image.jpg中的文件
我做了这个方法:
public static Boolean generateZipFile(ArrayList<String> sourcesFilenames, String destinationDir, String zipFilename){
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// VER SI HAY QUE CREAR EL ROOT PATH
boolean result = (new File(destinationDir)).mkdirs();
String zipFullFilename = destinationDir + "/" + zipFilename ;
System.out.println(result);
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFullFilename));
// Compress the files
for (String filename: sourcesFilenames) {
FileInputStream in = new FileInputStream(filename);
// Add ZIP entry to …Run Code Online (Sandbox Code Playgroud) 什么原因导致Tomcat出现这种错误?
SEVERE: Exception loading sessions from persistent storage
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException:
bean.ProjectAreaBean
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at java.util.ArrayList.readObject(ArrayList.java:593)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
Run Code Online (Sandbox Code Playgroud) 我有两个Java.io.File对象file1和file2.我想将内容从file1复制到file2.有没有标准的方法来做到这一点,我不必创建一个读取file1和写入file2的方法
我需要在我的应用程序中使用全局权限在myapp/files/subdir下创建文件.我这样做是因为我使用外部应用程序来打开一些文件
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
Run Code Online (Sandbox Code Playgroud)
仅在files文件夹下创建文件.运用
File dir=new File(Constants.TASK_DIRECTORY);
dir.mkdirs();
File file=new File(dir, FILENAME);
file.createNewFile(); FileOutputStream fos=new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)
在子目录下创建文件但具有私有权限.我需要找到一种方法来组合这两者,以便在子目录中创建一个世界可读的文件
我一直在尝试很多事情,但没有人帮助我,这是我未解决的最长时间问题
这可能是一个愚蠢的,但我想知道背景操作的差异.
InputStream is = new FileInputStream(filepath);FileInputStream is = new FileInputStream(filepath);上面两行代码之间的区别是什么,以及它们使用的场景.
有没有办法InputStream包装UTF-8列表String?我想做点什么:
InputStream in = new XyzInputStream( List<String> lines )
Run Code Online (Sandbox Code Playgroud) 我正在使用Java 7 File API.我编写了一个在Ubuntu上完美地创建目录的类,但是当我在Windows上运行相同的代码时,它会抛出错误:
Exception in thread "main" java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute
at sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source)
at java.nio.file.Files.createDirectory(Unknown Source)
at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source)
at java.nio.file.Files.createDirectories(Unknown Source)
at com.cloudspoke.folder_permission.Folder.createFolder(Folder.java:27)
at com.cloudspoke.folder_permission.Main.main(Main.java:139)
Run Code Online (Sandbox Code Playgroud)
我的文件夹类代码是
package com.cloudspoke.folder_permission;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.Set;
public class Folder{
// attributes required for creating a Folder
private UserPrincipal owner;
private Path folder_name;
private FileAttribute<Set<PosixFilePermission>> attr;
public Folder(UserPrincipal owner,Path folder_name,FileAttribute<Set<PosixFilePermission>> attr){
this.owner=owner;
this.folder_name=folder_name;
this.attr=attr;
}
//invoking …Run Code Online (Sandbox Code Playgroud) 我有一个目录:
File dir = new File(MY_PATH);
Run Code Online (Sandbox Code Playgroud)
我想列出名称为整数字符串的所有文件,例如"10","20".
我知道我应该使用:
dir.list(FilenameFilter filter);
Run Code Online (Sandbox Code Playgroud)
如何定义我的FilenameFilter?
PS我的意思是文件名可以是任何整数字符串,例如"10"或"2000000"或"3452345".只要文件名是整数字符串,就没有数字限制.
尝试在Java中java.nio.file.DirectoryNotEmptyException递归删除从Delete目录中删除的递归删除方法中的偶然问题
代码(归功于@TrevorRobinson):
static void removeRecursive(Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
logger.warn("Deleting " + file.getFileName());
Files.delete(file);
logger.warn("DELETED " + file.getFileName());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
// try to delete the file anyway, even if its attributes could
// not be read, since delete-only access is theoretically possible
// I NEVER SEE …Run Code Online (Sandbox Code Playgroud)