标签: fileoutputstream

ObjectOutputStream的writeObject方法使用什么字符编码?

我读到Java在内部使用UTF-16编码。即我明白,如果我喜欢:String var =“ ????”; 然后是“ ????” 将在内部以UTF-16编码。因此,如果我将此变量转储到某些文件中,例如:

fileOut = new FileOutputStream("output.xyz");
out = new ObjectOutputStream(fileOut);
out.writeObject(var);
Run Code Online (Sandbox Code Playgroud)

字符串“ ????”的编码 在文件“ output.xyz”中是否为UTF-16?另外,稍后,如果我想通过ObjectInputStream从文件“ output.xyz”中读取信息,是否可以获取该变量的UTF-16表示形式?

谢谢。

java objectoutputstream utf-16 character-encoding fileoutputstream

4
推荐指数
2
解决办法
6901
查看次数

Android:更快的方式来读/写ZipInputStream?

我目前正在编写一个应用程序,它在我的资源文件夹中读取一个zip文件,其中包含一堆图像.我正在使用ZipInputStreamAPI来读取内容,然后将每个文件写入my:Environment.getExternalStorageDirectory()目录.我有一切正常,但第一次运行应用程序将图像写入存储目录是不可思议的慢.将我的图像写入光盘大约需要5分钟.我的代码看起来像这样:

ZipEntry ze = null;
ZipInputStream zin = new ZipInputStream(getAssets().open("myFile.zip"));
String location = getExternalStorageDirectory().getAbsolutePath() + "/test/images/";

    //Loop through the zip file
    while ((ze = zin.getNextEntry()) != null) {
         File f = new File(location + ze.getName());
          //Doesn't exist? Create to avoid FileNotFoundException
          if(f.exists()) {
             f.createNewFile();
          }
          FileOutputStream fout = new FileOutputStream(f);
          //Read contents and then write to file
          for (c = zin.read(); c != -1; c = zin.read()) {
             fout.write(c);
          }             
    }
    fout.close();
    zin.close();
Run Code Online (Sandbox Code Playgroud)

读取特定条目的内容然后写入它的过程非常慢.我认为这更多的是阅读而不是写作.我已经读过你可以使用byte[]数组缓冲来加速这个过程,但这似乎不起作用!我尝试了这个,但它只读取了部分文件... …

android zipinputstream fileoutputstream

4
推荐指数
2
解决办法
5302
查看次数

Java FileOutputStream:相对于程序文件夹的路径?

查找相对于“安装”Java 应用程序的文件夹的路径的最佳方法是什么?

我有一个带有静态方法的类: public static void saveToFile(String fileName)

当我用绝对路径调用它时,它可以工作,但我真正想要的是运行应用程序的位置的相对路径和文件夹。

我还没有部署我的应用程序,但现在我想找到一个相对于(Netbeans)项目根目录的路径,以及一个名为 data 的文件夹:ProjectName\data\file.dat。我应该使用这个File类还是把它变成一个URI或什么的?

请注意,我更喜欢它独立于系统,并且在部署应用程序时仍然可以工作。最终(相对)路径名将存储在属性文件中。

对不起,如果这个问题是重复的,任何帮助表示赞赏。

java file path fileoutputstream

4
推荐指数
1
解决办法
2万
查看次数

序列化和简单地将对象存储在磁盘上有什么区别?

我对此感到困惑。因为在实现 Serializable 类时,我们需要使用类似的类FileOutputStreamObjectOutputStream或者类似的东西。那我们为什么不直接用这些类来做输出对象到文件、从文件输入对象这样的事情,直接维护对象的状态呢?为什么我们要先实现 Serializable 然后再做同样的事情?

java serialization objectoutputstream serializable fileoutputstream

4
推荐指数
1
解决办法
4974
查看次数

如何使用android存储访问框架正确覆盖文件内容

>> 背景

我想使用 SAF(存储访问框架字)将我的应用程序的数据文件保存到用户在存储介质上所需的位置。我首先在应用程序专用文件夹中创建文件,然后将其复制到用户从文件选择器对话框中选择的文件(代码稍后发布)。

此过程适用于新文件但对于现有文件,尽管文件选择器会警告覆盖文件,但在写入之前不会删除最终文件。

通过计算写入的字节数并使用十六进制编辑器调查文件,代码将正确的字节写入输出流,但是:如果现有文件的字节数多于要写入的字节数,则最终覆盖的文件已损坏(实际上并未损坏,请参阅下一节进行澄清),如果现有的字节数少于要写入的字节数,则最终覆盖的文件是正确的。

>> 更多细节和代码

我使用下面的代码来显示问题(jpg 为示例):我将尝试使用两个文件:

file1.jpg 166,907 bytes
file2.jpg 1,323,647 bytes
file3.jpg The final file with variable size
Run Code Online (Sandbox Code Playgroud)

首先,我将把 file1 复制到用户选择的名为 file3 的文件夹(目标文件),然后用 file2 覆盖它,最后我将用 file1 再次覆盖它。看看代码是什么以及会发生什么:

调用文件选择器的代码:

val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
    addCategory(Intent.CATEGORY_OPENABLE)
    type = "image/jpeg"
}
startActivityForResult(intent, request)
Run Code Online (Sandbox Code Playgroud)

现在在 onActivityResult() 我处理数据如下:

contentResolver.openOutputStream(fileUri)?.use {output->
        val input = FileInputStream(File(getExternalFilesDir(null),"Pictures/file1.jpg"))
    // file1.jpg for first run, file2.jpg for 2nd run and file1.jpg again for 3rd run     
        copyStream(input, output)

    }
Run Code Online (Sandbox Code Playgroud)

以及复制流的代码:

@Throws(IOException::class)
fun copyStream(input: InputStream, output: …
Run Code Online (Sandbox Code Playgroud)

android overwrite fileoutputstream storage-access-framework

4
推荐指数
2
解决办法
1655
查看次数

如何使用 Intent.ACTION_CREATE_DOCUMENT 编写文件

简单来说,我想将一个视图组转换为一个 jpg 图像文件。由于Environment.getExternalStorageDirectory已弃用,我使用此意图Intent.ACTION_CREATE_DOCUMENT

private void createFile(String mimeType, String fileName) {
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(mimeType);
        intent.putExtra(Intent.EXTRA_TITLE, fileName);
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    }
Run Code Online (Sandbox Code Playgroud)

onActivityResult();我得到结果返回的 Uri。 我的问题是getExternalStorage()我会用

Bitmap bitmap = Bitmap.createBitmap(
                    containerLayout.getWidth(),
                    containerLayout.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            containerLayout.draw(canvas);
            FileOutputStream fileOutupStream = null;


            try {
                fileOutupStream = new FileOutputStream(fileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutupStream);
                fileOutupStream.flush();
                fileOutupStream.close();
                Toast.makeText(this, "saved " + fileName, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "something went wrong" + e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            } …
Run Code Online (Sandbox Code Playgroud)

android bitmap fileoutputstream android-intent

4
推荐指数
1
解决办法
5459
查看次数

java.io.FileNotFoundException:(只读文件系统)Mac

我有一个 SpringBoot 应用程序,我试图在其中测试条形码的生成,但出现此错误java.io.FileNotFoundException: (Read-only file system) Mac

\n\n

这是完成此任务的代码:

\n\n

pom.xml

\n\n
        <dependency>\n            <groupId>junit</groupId>\n            <artifactId>junit</artifactId>\n            <version>4.13</version>\n            <scope>test</scope>\n        </dependency>\n        <dependency>\n            <groupId>net.sf.barcode4j</groupId>\n            <artifactId>barcode4j</artifactId>\n            <version>2.1</version>\n        </dependency>\n
Run Code Online (Sandbox Code Playgroud)\n\n

Test Class

\n\n
public class FooTest extends TestCase {\n    @Test\n    public void testP() {\n        try {\n            Code128Bean bean = new Code128Bean();\n            final int dpi = 160;\n\n            //Configure the barcode generator\n            bean.setModuleWidth(UnitConv.in2mm(2.8f / dpi));\n\n            bean.doQuietZone(false);\n\n            //Open output file\n            File outputFile = new File("/" + "test" + ".JPG");\n\n            FileOutputStream out = new FileOutputStream(outputFile);\n\n            BitmapCanvasProvider canvas = …
Run Code Online (Sandbox Code Playgroud)

java spring file barcode fileoutputstream

4
推荐指数
1
解决办法
2万
查看次数

在java中为什么FileWriter抛出IOException而FileOutputStream抛出FileNotFoundException的原因完全相同

来自java文档

public FileWriter(String fileName)抛出IOException

抛出:

IOException - 如果指定的文件存在但是是目录而不是常规文件,则不存在但无法创建,或者由于任何其他原因无法打开

public FileOutputStream(File file,boolean append)抛出FileNotFoundException

抛出:

FileNotFoundException - 如果文件存在但是是目录而不是常规文件,则不存在但无法创建,或者由于任何其他原因无法打开

这个选择有特定原因吗?

java filenotfoundexception ioexception filewriter fileoutputstream

3
推荐指数
1
解决办法
1297
查看次数

使用FileStream在Java中复制文件

我想使用FileStream在Java中复制文件.这是我的代码.

FileInputStream infile = new FileInputStream("in");
FileOutputStream outfile = new FileOutputStream("out");

byte[] b = new byte[1024];
while(infile.read(b, 0, 1024) > 0){
    outfile.write(b);
}

infile.close();
outfile.close();
Run Code Online (Sandbox Code Playgroud)

我使用vim来查看我的文件.
输入文件"in"

Hello World1
Hello World2
Hello World3
Run Code Online (Sandbox Code Playgroud)

输出文件"输出"

Hello World1
Hello World2
Hello World3
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@...
Run Code Online (Sandbox Code Playgroud)

输出文件中有许多额外的'^ @'.
输入文件的大小为39字节.
输出文件的大小为1KB.
为什么输出文件中有很多额外的字符?

java fileinputstream fileoutputstream

3
推荐指数
1
解决办法
2581
查看次数

BufferedOutputStream不会抛出I/O异常

在工作时BufferedOutputStream发现它并没有抛出IOException我们在关闭流后写的时候.

为了验证我的结果,我检查FileOutputStream发现IOException一旦我们在关闭它之后尝试写它就扔了.

public class Test {
    public static void main(String[] args) {
        try {
            // Created a byte[] barry1 barry2
            byte[] barry1 = { '1', '3' };
            byte[] barray2 = { '2', '4' };
            OutputStream os = new BufferedOutputStream(
                  new FileOutputStream("abc.txt", false));
            // Writing to stream
            os.write(barry1);
            os.close();
            os.write(barray2); // this suceeds - bug

            os = new FileOutputStream("abc.txt", true);
             //Writing to stream
            os.write(barry1);
            os.close();
            os.write(barray2); // crashes here, correct.
        } catch (Exception …
Run Code Online (Sandbox Code Playgroud)

java file-io bufferedinputstream fileoutputstream

3
推荐指数
1
解决办法
619
查看次数