如何在Windows中创建具有属性FILE_ATTRIBUTE_TEMPORARY并FILE_FLAG_DELETE_ON_CLOSE使用Java设置的文件?
我确实希望我的文件只是内存文件。
精确地说:“退出时删除”机制令我不满意,因为我想避免这种情况,例如在应用程序崩溃的情况下,某些数据留在磁盘上。
我知道如何截断RandomAccess文件,以便删除末尾的字节.
raf.getChannel().truncate(file.length() - 4);
Run Code Online (Sandbox Code Playgroud)
要么
raf.setLength(file.length() - 4);
Run Code Online (Sandbox Code Playgroud)
但是如何以一种开头的字节被删除的方式截断RandomAccessFile?我不需要将此文件的内容写入新文件.我用Google搜索,无法找到答案.请帮忙.提前致谢.
我有一个Jar文件,我使用第三方库创建.当我打包jar文件时,我将几个xml文件包含在名为data的文件夹中
data
- file1.xml
- file2.xml
- file3.xml
Run Code Online (Sandbox Code Playgroud)
现在,我想读取jar文件中的文件夹,根据第三方库的文档,我可以获得类加载器,并像这样读取文件夹作为输入流.
ClassLoader clsLoader = myService.getClassLoader();
InputStream accountsStream = clsLoader.getResourceAsStream("data");
Run Code Online (Sandbox Code Playgroud)
问题是,如何列出输入流中的所有文件并逐个解析?
谢谢
编辑 补充信息:
How do I access resources that I put into my service or module archive file?
Run Code Online (Sandbox Code Playgroud)
http://axis.apache.org/axis2/java/core/faq.html#b1
对不起,这个问题应该是针对Apache Axis的,但如果它也是一个特定于Java的问题,我会感到困惑.
使用类加载器获取文件夹的输入流后,如何将所有文件列入该文件夹并逐个读取?
我的代码中的步骤将包括在内.
我在SO上提到了一些类似的问题,但没有一个涉及IO.
我使用时在java中使用了相同的代码Eclipse.那个时候它奏效了.
但现在我尝试在Mono for Android(C#)中使用此代码,它不起作用.
我正在尝试运行此代码来创建InputStream:
InputStream myInput =ctx.Assets.Open(DATABASE_NAME + ".db");
Run Code Online (Sandbox Code Playgroud)
但它给了我编译时错误: Cannot implicitly convert type 'System.IO.Stream' to 'Java.IO.InputStream'
有一个直接的功能可以将文件从资源复制到设备内存,但需要源和目标路径.
我如何获得源路径???
因为我是绝对的初学者Mono for Android,任何帮助表示赞赏.
我正在编写一个Android应用程序,我需要从几个文件夹中读取几个文件并将它们添加到几个zip存档中.我需要限制档案的最大大小,比方说16mb.因此,在运行时,如果文件的大小超过16 MB,则将文件添加到存档时创建具有相同大小限制的另一个存档,依此类推.我正在使用以下包装类:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ChunkedZippedOutputStream {
private ZipOutputStream zipOutputStream;
private String path;
private String name;
private long currentSize;
private int currentChunkIndex;
private final long MAX_FILE_SIZE = 16 * 1000 * 1024; // 16mb limit
private final String PART_POSTFIX = ".part";
private final String FILE_EXTENSION = ".zip";
public ChunkedZippedOutputStream(String path, String name) throws FileNotFoundException {
this.path = path;
this.name = name;
constructNewStream();
}
public void addEntry(ZipEntry entry) throws IOException …Run Code Online (Sandbox Code Playgroud) 我正在编写一个java程序来读取进程中的错误流.以下是我的代码结构 -
ProcessBuilder probuilder = new ProcessBuilder( command );
Process process = probuilder.start();
InputStream error = process.getErrorStream();
InputStreamReader isrerror = new InputStreamReader(error);
BufferedReader bre = new BufferedReader(isrerror);
while ((linee = bre.readLine()) != null) {
System.out.println(linee);
}
Run Code Online (Sandbox Code Playgroud)
如果实际将任何内容写入调用进程的错误流,则上述代码可以正常工作.但是,如果没有任何内容写入错误流,则对readLine的调用实际上会无限期挂起.但是,我想让我的代码通用,以便适用于所有场景.如何修改我的代码以实现相同的目标.
问候,开发
我正在运行一个线程,并且每次运行时都应该检查是否有新行要从中读取,BufferedReader但是它会等待一条线存在,从而停止整个代码.
if((inputLine = bufferedReader.readLine()) != null){
System.out.println(inputLine);
JOptionPane.showMessageDialog(null, inputLine);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法更好地检查是否有文本BufferedReader要读取?
什么:
System.in.read()
Run Code Online (Sandbox Code Playgroud)
回来?该文件 说:
返回:数据的下一个字节,如果到达流的末尾,则返回-1.
但是,例如,如果我进入:10我回来了49.这是为什么 ?
我试图了解以下行为。我的旧代码,
String path = "C:/temp/sample.txt";
String mode= "rw";
FileChannel channel = new RandomAccessFile(path, mode).getChannel();
// some code to write to this file
// finally delete
File file = new File(path);
boolean isDeleted = file.delete();
System.out.println("Is Deleted - " + isDeleted);
Run Code Online (Sandbox Code Playgroud)
O / P-已删除-否
仅当我执行“ channel.close();”时 在我删除文件之前。它是否删除文件并返回true。
较新的替换代码,
String path = "C:/temp/sample.txt";
FileChannel fChannel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
// some code to write to this file
// finally delete
File file = new File(path);
boolean isDeleted = file.delete();
System.out.println("Is Deleted …Run Code Online (Sandbox Code Playgroud)