有没有办法以一种方式使用FileOutputStream,如果文件(String filename)不存在,那么它会创建它吗?
FileOutputStream oFile = new FileOutputStream("score.txt", false);
我正在尝试删除一个文件,在写完文件后,用FileOutputStream.这是我用来编写的代码:
private void writeContent(File file, String fileContent) {
    FileOutputStream to;
    try {
        to = new FileOutputStream(file);
        to.write(fileContent.getBytes());
        to.flush();
        to.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
如图所示,我刷新并关闭流,但是当我尝试删除时,file.delete()返回false.
我删除前检查,看看是否该文件存在,并且:file.exists(),file.canRead(),file.canWrite(),file.canExecute()所有返回true.在调用这些方法后,我尝试file.delete()返回false.
有什么我做错了吗?
如果使用FileOutputStream方法,每次通过这种方法编写文件时,都会丢失旧数据.是否可以通过写入文件而不丢失旧数据FileOutputStream?
我在从我的应用程序中下载二进制文件(视频)时遇到问题.在Quicktime中,如果我直接下载它可以正常工作但通过我的应用程序不知何故它搞砸了(即使它们在文本编辑器中看起来完全相同).这是一个例子:
    URL u = new URL("http://www.path.to/a.mp4?video");
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
    InputStream in = c.getInputStream();
    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ( (len1 = in.read(buffer)) > 0 ) {
         f.write(buffer);
    }
    f.close();
我试图使用整数数组保存一些块覆盖,这只是节省块执行的次数.但是,出于某种原因,当我尝试写入我创建的一些文件时(例如,我在Eclipse中专门制作并放在项目目录中的"BlockForHelper.txt"),我收到此错误:
java.io.FileNotFoundException:  /nfs/guille/groce/users/nicholsk/workspace3/SQLTest/BlockForTest: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
at com.example.sql2.SQLTest.blockCoverage(SQLTest.java:149)
at com.example.sql2.test.SQLTestCase.testSuite(SQLTestCase.java:41)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
... 18 more
并给我错误:
public void blockCoverage() throws IOException
{
    String coverage = "";
    for (int x = 0; x < 20; x++)
        coverage …我有一个模块负责读取,处理和写入磁盘的字节.字节通过UDP传入,在汇编各个数据报之后,处理并写入磁盘的最终字节数组通常在200字节到500,000字节之间.偶尔会有一些字节数组,在汇编后,超过500,000字节,但这些数组相对较少.
我现在正在使用FileOutputStream的write(byte\[\])方法.我也在尝试包装FileOutputStreamin BufferedOutputStream,包括使用接受缓冲区大小作为参数的构造函数.
似乎使用的BufferedOutputStream是趋向于略微更好的性能,但我只是开始尝试不同的缓冲区大小.我只有一组有限的样本数据可供使用(来自样本运行的两个数据集,我可以通过我的应用程序管道).是否有一般的经验法则我可以应用于尝试计算最佳缓冲区大小以减少磁盘写入并最大化磁盘写入的性能,因为我知道有关我正在编写的数据的信息?
有没有办法从一个FileOutputStream或从中获取文件名FileInputStream?
我正在尝试从互联网上下载一个iamge,这是代码:
try {
                String imgURL = c.imgURL;
                String imgPATH = c.imgPATH;
                URL url = new URL(imgURL);
                URLConnection conexion = url.openConnection();
                conexion.connect();
                int lenghtOfFile = conexion.getContentLength();
                try {
                    File f = new File(imgPATH);
                    f.mkdirs();
                    BufferedInputStream input = new BufferedInputStream(url.openStream());
                    BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(imgPATH), 8192); // CRASH HERE
                    byte data[] = new byte[8192];
                    long total = 0;
                    int count = 0;
                    int updateUILimiter = 0;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        if (updateUILimiter == 20)
                            // …基本上,我必须通过Java应用程序覆盖.properties文件中的某个属性,但是当我使用Properties.setProperty()和Properties.Store()时,它会覆盖整个文件而不是仅覆盖那个属性.
我尝试使用append = true构建FileOutputStream,但是它添加了另一个属性,并且不会删除/覆盖现有属性.
我如何对其进行编码,以便设置一个属性会覆盖该特定属性,而不会覆盖整个文件?
编辑:我尝试读取文件并添加到它.这是我更新的代码:
FileOutputStream out = new FileOutputStream("file.properties");
FileInputStream in = new FileInputStream("file.properties");
Properties props = new Properties();
props.load(in);
in.close();
props.setProperty("somekey", "somevalue");
props.store(out, null);
out.close();
我想在每天运行Hadoop作业时覆盖/重用现有的输出目录.实际上,输出目录将存储每天作业运行结果的汇总输出.如果我指定相同的输出目录,则会给出错误"输出目录已存在".
如何绕过此验证?
fileoutputstream ×10
java ×8
android ×2
file ×2
file-io ×2
io ×2
download ×1
eclipse ×1
hadoop ×1
new-operator ×1
properties ×1
rewrite ×1