改变字节时复制文件无法正常工作 [英] changing byte when copy file not working

查看:115
本文介绍了改变字节时复制文件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字节改变时,副本文件形式一个位置到另一个不工作什么是错在我的code,请帮助我,如果我删除字符串S1;那么它的工作完美的只是复制原始文件时,我使用S1字符串的不行改变字节

  samplet.text文件包含数字
            3434214280
            3044559080
             3154356865
          3312430124
         3334491537
                 包com.example.copyfilefromdirectorytoanother;
   ublic类MainActivity延伸活动{私有静态最后弦乐TAG =MainActivity.java;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    //你的SD卡
    。字符串SD卡= Environment.getExternalStorageDirectory()的toString();    //文件被移动或复制
    文件sourceLocation =新的文件(SD卡+/sample.txt);    //确保您的目标位置的文件夹存在!
    文件targetLocation =新的文件(SD卡+/MyNewFolder/sample.txt);    //只是注意的位置信息源
    Log.v(TAG,sourceLocation:+ sourceLocation);
    Log.v(TAG,targetLocation:+ targetLocation);    尝试{        // 1 =移动文件,2 =复制文件
        INT actionChoice = 2;        //将文件移动到另一个目录
        如果(actionChoice == 1){            如果(sourceLocation.renameTo(targetLocation)){
                Log.v(TAG,移动文件成功。);
            }其他{
                Log.v(TAG,移动文件失败。);
            }        }        //我们将复制的文件
        其他{            //确保目标文件存在            如果(sourceLocation.exists()){                在的InputStream =新
 的FileInputStream(sourceLocation);
                出的OutputStream新=
    FileOutputStream中(targetLocation);                //位从河道内复制到outstream
                串S;
            //字节[] theByteArray;
                字节[] buf中=新的字节[1024];
                字节[] = theByteArray新的字节[1024];
                INT LEN;
                INT N = 1;
                而((LEN = in.read(BUF))大于0){
S1 =BEGIN:VCARD \\ n版本:2.1 \\ n N:; UNKNOWN+ N +;;; \\ n FN:UNKNOWN+ N +
 \\ n TEL; CELL; preF的:+92+ BUF +\\ n END:VCARD
                    theByteArray = s1.getBytes();                    out.write(theByteArray,0,LEN);
                    N = N + 1;
                }                附寄();
                out.close();                Log.v(TAG,复制文件成功。);            }其他{
                Log.v(TAG,复制文件失败源文件
       失踪。);
            }        }    }赶上(NullPointerException异常五){
        e.printStackTrace();
    }赶上(例外五){
        e.printStackTrace();
    }
}
}


解决方案

那么,你正在阅读的len个字节,数据附加到它,然后写出来len个字节。这是不对的,你需要做的 out.write(theByteArray,0,theByteArray.length); 我可以看到其他可能出现的问题有太多,但你的文件可能是小到避开他们。

changing byte when copy file form one location to another not working whats wrong in my code please help me if i remove string s1; then its work perfect just duplicate original file when i alter bytes using s1 string its not work

 samplet.text file contain numbers


            3434214280
            3044559080
             3154356865
          3312430124
         3334491537
                 package com.example.copyfilefromdirectorytoanother;


   ublic class MainActivity extends Activity {

private static final String TAG = "MainActivity.java";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // your sd card
    String sdCard = Environment.getExternalStorageDirectory().toString();

    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");

    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

    // just to take note of the location sources
    Log.v(TAG, "sourceLocation: " + sourceLocation);
    Log.v(TAG, "targetLocation: " + targetLocation);

    try {

        // 1 = move the file, 2 = copy the file
        int actionChoice = 2;

        // moving the file to another directory
        if(actionChoice==1){

            if(sourceLocation.renameTo(targetLocation)){
                Log.v(TAG, "Move file successful.");
            }else{
                Log.v(TAG, "Move file failed.");
            }

        }

        // we will copy the file
        else{

            // make sure the target file exists

            if(sourceLocation.exists()){

                InputStream in = new 
 FileInputStream(sourceLocation);
                OutputStream out = new 
    FileOutputStream(targetLocation);

                // Copy the bits from instream to outstream
                String s1;
            //  byte[] theByteArray ;
                byte[] buf = new byte[1024];
                byte[] theByteArray = new byte[1024];
                int len;
                int n =1;
                while ((len = in.read(buf)) > 0) {


s1= "BEGIN:VCARD \n VERSION:2.1 \n N:;UNKNOWN "+n+";;; \n FN:UNKNOWN "+n+" 
 \n TEL;CELL;PREF:+92"+buf+" \n     END:VCARD ";
                    theByteArray=s1.getBytes();

                    out.write(theByteArray, 0, len);
                    n=n+1;
                }

                in.close();
                out.close();

                Log.v(TAG, "Copy file successful.");

            }else{
                Log.v(TAG, "Copy file failed. Source file 
       missing.");
            }

        }

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

解决方案

Well, you're reading in len bytes, appending data to it, then writing out len bytes. That's wrong, you need to do out.write(theByteArray, 0, theByteArray.length); I can see other possible problems there too, but your file may be small enough to avoid them.

这篇关于改变字节时复制文件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆