2011-02-02 110 views
0

我在从Flex读取文件时遇到问题。该文件包含一个base64编码的字符串。当我读取文件,我得到的长度为47856和解码base64字节数组长度为34157.Flex文件读取问题

当我从java读取相同的文件我得到的长度分别为48068和35733。

什么问题?

private function init():void{ 
     var file:File = File.desktopDirectory.resolvePath("Files/sample.txt"); 
     stream = new FileStream(); 
     stream.open(file, FileMode.READ); 
     var str:String = stream.readUTFBytes(stream.bytesAvailable); 
     stream.close(); 
     str = str.replace(File.lineEnding, "\n"); 
     contents.text = str; 
     fileName.text = file.name; 
    } 


public function playSound(contents:String):void{ 



    try{ 
     var byteData: ByteArray; 

     byteData = new ByteArray(); 
     byteData.writeUTFBytes(contents); 
     var dec:Base64Decoder = new Base64Decoder(); 
     dec.decode(contents); 
     byteData = dec.toByteArray(); 



     Alert.show("byte Array " + byteData.toString().length +" :: " +contents.length); 


    } 

这是我的Java代码阅读文件...无论我期待的结果是在java端实现。

private static String readFile(String path) throws IOException { 
     FileInputStream stream = new FileInputStream(new File(path)); 
     try { 
      FileChannel fc = stream.getChannel(); 
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 

      return Charset.defaultCharset().decode(bb).toString(); } 
     finally {  stream.close(); 
     } 
    } 

Java代码的我在哪里打印长度

byte[] decodedBase64 = new byte[byteLength]; 
       String speexData = null; 
       try { 
        speexData = readFile(userDir +"//" +xmlFileName); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 

       // System.out.println("sa " + sa); 

       try{ 

        decodedBase64= Base64.decodeToByteArray(speexData); 
        System.out.println("decodednase64 length " + decodedBase64.length +" :: " +speexData.length()); 
       } 
       catch(Exception e){ 
       } 

回答

0

你将不得不发布Java代码,以显示自己在做什么都有,还有。但是,如果不知道更多,我可以猜测并说,当您替换行尾时,您可能每次都删除一个字节(如果它是\ r \ n并且您正在创建它\ n,例如)。

+0

@上一篇:我编辑了我的帖子 – karthick 2011-02-02 09:20:09