先把list里面的东西拿出来,转成String类型的
String 类有个 getBytes[] 方法, 会将字符串转换成byte数组

解决方案 »

  1.   

    BufferedWriter不是有write(String str)方法?
    不管是什么list总是可以遍历得到数据,for example
    List<String> list = new LinkedList<String>();
    for(String str :list){
    //doSomething
    }
    如果真要传btye数组,先把list的东西拿出来,然后在调用String.getBytes()方法
      

  2.   

    String[] s=(String[])stuList.toArray(new String[stuList.size()]);
    我是这样转换的,虽然写出来没报错,但是一运行就报错!是一种转换错误!
      

  3.   

    我之前的list里面有值,如果List<String> list = new LinkedList<String>();
    这样会不会把我之前的list里面清空了?新手   麻烦指导下
    谢谢
      

  4.   

    List list = yourList; 不就行了?
      

  5.   

    这样   写不下去了byte[] bytes=new byte[]{};
       //String[] s=(String[])stuList.toArray(new String[stuList.size()]);
       List<String> list=stuList;
      for(String str:list)
      {
      bytes=str.getBytes();
      }
      BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\out.txt"));
      

  6.   

    byte[] bytes = new bytes[]{}; //你写的是分配长度为0的byte数组,存不了数据。new byte[len];
    String[] s=(String[])stuList.toArray(new String[stuList.size()]);  //这行代码的强制转换时不需要的。
    BufferWriter有write字符串的方法。
      

  7.   

    stuList里面存的是Student,所以会报java.lang.ClassCastException: com.sh.test.Student这个错误,运行代码是
    BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\out.txt"));
       List<String> list=stuList;
       for(String str:list)
    {
    bw.write(str);
    System.out.println(str);
    }
      

  8.   

    晕。。你这样哪里可以转型成功。 List<Student> list=stuList;
     for(Student student:list)
            {
                bw.write(str);
                System.out.println(str);
            }或则for(Student stduent : stuList){
    //doSomething
    }
      

  9.   

    这样写的话,可以在Student里实现toString方法,就可以直接bw.write(student)了,不然打印的会是类的信息哦。
      

  10.   

    可以将list转换为JSON字符串再getBytes,再写进去,取的时候byte[]转String再转为List。或者更简单的直接用ObjectInputStream将对象写进去,读的时候再用ObjectOutputStream读取出来就好了。
    例:
    public static void main(String[] args) {
    write();
    read();
    }

    public static void write(){
    List<String> list = new ArrayList<String>();
    list.add("buer");
    list.add("布尔");
    list.add("布爾");
    try {
    OutputStream os = new FileOutputStream("d:\\1.dat");
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(list);
    oos.close();
    os.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void read(){
    Object obj = null;
    try {
    InputStream is = new FileInputStream("d:\\1.dat");
    ObjectInputStream ois = new ObjectInputStream(is);
    obj = ois.readObject();
    ois.close();
    is.close();
    } catch (Exception e) {
    e.printStackTrace();
    }

    List<String> list = (List<String>) obj;
    for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
    }
    }
      

  11.   

    public static byte[] objectToByteArray(Object obj) throws Exception {
    byte[] bytes = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.close();
    bytes = baos.toByteArray();
    baos.close();
    return bytes;
    } public static Object byteArrayToObject(byte[] buffer) throws Exception {
    Object ob = null;
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
    buffer));
    ob = ois.readObject();
    ois.close();
    return ob;
    }