java怎么读取c++保存的结构体的文件,我用bufferReader读取的内容都不对
c++的结构体
struct TestS
{
int   nID;
int   nSize;
char  szTest[32];
};        test.nID=100;
test.nSize=1000;
strcpy(test.szTest,"1234567890中国!");
/**
     * 从文件中读取文本
     */
    public void readFromFile(String file) {
      
      try {
       String str="";
      BufferedReader bre = null;
      bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
      while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
      {
         System.out.println(str);
      }
      bre.close();
      }catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
      }
      
      }  

解决方案 »

  1.   

    这样就OK了,可以使用commmon.io或guava.jar,合理使用第三方开源的jar,代码也少几行,出错率也会小
    public static String readFile(String path, String encoding) {
            File file = new File(path);  
            String fileContent = null;  
            try {  
             if(file.exists()) {
             fileContent = FileUtils.readFileToString(file, encoding);
             }
            } catch (IOException e) {  
                e.printStackTrace();  
            } 
            return fileContent;
    }