标签: fileoutputstream

在java中,如何从输入流中读取固定长度并保存为文件?

在java中,如何从输入流中读取固定长度并保存为文件?例如。我想从 inputStream 读取 5M,并保存为 downloadFile.txt 或其他内容。(BUFFERSIZE=1024)

FileOutputStream fos = new FileOutputStream(downloadFile);
byte buffer [] = new byte[BUFFERSIZE];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1)
{
    fos.write(buffer, 0, temp);
}
Run Code Online (Sandbox Code Playgroud)

java inputstream outputstream fileinputstream fileoutputstream

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

从FileOutputStream创建文件

我正在使用org.apache.commons.net.ftp远程机器下载文件.有一种方法可以将文件读取到FileOutputStream对象中.

ftpClient.retrieveFile("/" + ftpFile.getName(), fos);
Run Code Online (Sandbox Code Playgroud)

问题,这是,我有另一个接受File对象的方法.所以,我需要创建一个File目标文件FileOutputStream.我想,我需要创建一个InputStream能够从中创建文件对象FileOutputStream.它是否正确?我可能会遗漏一些东西,应该有一个简单的方法来创建File一个FileOutputStream

java file-io fileoutputstream

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

FileOutputStream中的新行

下一行的Ascii值是10.所以我尝试了这个......

 FileOutputStream os = new  FileOutputStream(f, true);
    os.write(10);  // this should get me to next line ?
    os.write(b);   // b is a byte array...
Run Code Online (Sandbox Code Playgroud)

java outputstream fileoutputstream

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

FileOutputStream和PrintWriter之间的区别

我正在我的应用程序中实现SSL证书和密钥.我使用CertAndKeyGen类创建了私钥.我试图用密码加密私钥,我通过PBE和Cipher类实现了它.我想将加密的私钥写入PEM格式的文​​件中.我尝试使用FileOutputStream,但PrintWriter不能正常工作.

以下是我的代码,

    final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
    keypair.generate(1024);
    final PrivateKey privKey = keypair.getPrivateKey();
    byte[] encodedprivkey = privKey.getEncoded();
    String MYPBEALG = "PBEWithSHA1AndDESede";
    String password = "test123";
    int count = 20;// hash iteration count
    Random random = new Random();
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(MYPBEALG);
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(MYPBEALG);
    // Initialize PBE Cipher with key and parameters
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); …
Run Code Online (Sandbox Code Playgroud)

java fileoutputstream printwriter

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

android将日志语句写入sdcard

我正在尝试将日志语句写入SD卡.我决定这样做的方法是通过Application Object在SD卡上创建一个文件.这样我就可以从应用程序的任何地方调用静态方法logToSdcard().

创建了包含文件夹"/ RR3log /",但我记录的每个语句都在其自己的文件中,名为"rr3LogFile.txt".所以我有多个rr3LogFile文件,每个文件包含一个staement.

如何将所有语句写入一个rr3LogFile文件?在此先感谢马特.

public class NfcScannerApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();




        File storageDir = new File(Environment
                .getExternalStorageDirectory(), "/RR3log/");


        storageDir.mkdir();
        try {

            if(outfile == null){
            outfile=File.createTempFile("rr3LogFile", ".txt",storageDir);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }





public static void logToSdcard(String tag, String statement){


        Log.e(TAG, "inside logtosdcard$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");


                String state = android.os.Environment.getExternalStorageState();
                if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
                    try {
                        throw new IOException("SD Card is not mounted.  It is " + state + ".");
                    } catch …
Run Code Online (Sandbox Code Playgroud)

android sd-card fileoutputstream

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

使用FileOutputStream通过进度条下载Android ION

我正在尝试使用progressbar修改下载示例中的koush代码,使其写入FileOutputStream而不是File,但eclipse会给我以下错误:

对于ResponseFuture类型,方法progressHandler(new ProgressCallback(){})未定义

这是代码:

File file = new File(DownloadPath, uri.getLastPathSegment());
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
}

Future<FileOutputStream> downloading = Ion.with(getApplicationContext())
    .load(uri)
    .write(fos)
    .progressHandler(new ProgressCallback() { 
        @Override
        public void onProgress(int downloaded, int total) {
            // inform the progress bar of updates in progress
        }
    })
    .setCallback(new FutureCallback<FileOutputStream>() {
       @Override
        public void onCompleted(Exception e, FileOutputStream file) {
            // download done...
            // do stuff with the File or …
Run Code Online (Sandbox Code Playgroud)

android download fileoutputstream ion-koush

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

如何在Java中向字节数组添加换行符?

以下代码尝试将换行符引入字节数组并将字节数组写入文件。

import java.io.*;
public class WriteBytes {
    public static void main(String args[]) {
        byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\n', 'd', 'c' };
        FileOutputStream outfile = null;
        try {
            outfile = new FileOutputStream("newfile.txt");
            outfile.write(cities);
            outfile.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

新文件的内容是:newyorkdc

我所期望的是:
纽约
特区

我尝试打字'\n'(byte)'\n'无济于事。

解决方案:将数组初始化改为

byte[] cities = { 'n', 'e', 'w', 'y', 'o', 'r', 'k', '\r','\n', 'd', 'c' };
Run Code Online (Sandbox Code Playgroud)

我使用 Notepad++ 查看文件的内容。我想它抑制了换行符,但接受了回车符和换行符组合。

java arrays fileoutputstream

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

write(byte[],int,int) 方法是如何工作的?

public class JavaCopyFileProgram {



        public static void main(String[] args)
        {    
            File sourceFile = new File("F:/Study/Java/Java Programs/Factory Methods.txt");

            File destFile = new File("D:/DestFile.txt");

            FileInputStream inStream = null;

            FileOutputStream outStream = null;

            try
            {
                inStream = new FileInputStream(sourceFile);

                outStream = new FileOutputStream(destFile);

                byte[] buffer = new byte[1024];

                int length;

                while ((length = inStream.read(buffer)) != -1) 
                { 
                    outStream.write(buffer, 0, length);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    inStream.close();

                    outStream.close();
                }
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }

            System.out.println("Success"); …
Run Code Online (Sandbox Code Playgroud)

java fileinputstream fileoutputstream

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

为什么这个简单的可序列化对象抛出NotSerializableException?

在对我的上一个问题没有回答之后,我将这个问题重新表述为最简单的形式.1个按钮,1个clicklistener,1个可序列化对象和1个子程序,用于输出可序列化对象.此代码基于我在stackoverflow上找到的大约6-8个示例.但它仍然很简单它仍然会产生这个错误:W/System.err(228):java.io.NotSerializableException:serobj.testActivity所以我挑战你,哦,这么明智的代码大师:为什么这段代码会产生这个错误?最重要的是我该怎么做才能解决它?整个代码后跟log输出:

package serobj;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import ser.obj.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class testActivity extends Activity {
/** Called when the activity is first created. */
public class Tester implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
public String frog;
    public Tester(){
        frog="frog";
    }

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() …
Run Code Online (Sandbox Code Playgroud)

serialization android serializable fileoutputstream

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

将db从资产复制到设备数据文件夹时出现FileNotFoundException

我试过很多方法将我的sqlite db文件复制到我的/ data/data/packagename/databases文件夹,但是我仍然陷入FileNotFoundException,由FileOutputStream对象触发...这是代码:

public static boolean checkCopyDb(Context c, DbHandler _db) {
    try {
        String destPath = "/data/data/" + c.getPackageName() + "/databases/db_sociallibraries.db";

        File dbFile = new File(destPath);

        if(!dbFile.exists()) {
            _db.copyDb(c.getAssets().open(DB_NAME), new FileOutputStream(destPath)); // Line 44 - Throws the exception
        }
        return true;
    }
    catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

private void copyDb(InputStream inputStream, OutputStream outputStream) throws IOException {

    byte[] buffer = new byte[1024];
    int length;

    while((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer,0,length);
    }

    inputStream.close();
    outputStream.close();
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

02-09 …

sqlite android assets filenotfoundexception fileoutputstream

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