list转换成byte数组 先把list里面的东西拿出来,转成String类型的String 类有个 getBytes[] 方法, 会将字符串转换成byte数组 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BufferedWriter不是有write(String str)方法?不管是什么list总是可以遍历得到数据,for exampleList<String> list = new LinkedList<String>();for(String str :list){//doSomething}如果真要传btye数组,先把list的东西拿出来,然后在调用String.getBytes()方法 String[] s=(String[])stuList.toArray(new String[stuList.size()]);我是这样转换的,虽然写出来没报错,但是一运行就报错!是一种转换错误! 我之前的list里面有值,如果List<String> list = new LinkedList<String>();这样会不会把我之前的list里面清空了?新手 麻烦指导下谢谢 List list = yourList; 不就行了? 这样 写不下去了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")); byte[] bytes = new bytes[]{}; //你写的是分配长度为0的byte数组,存不了数据。new byte[len];String[] s=(String[])stuList.toArray(new String[stuList.size()]); //这行代码的强制转换时不需要的。BufferWriter有write字符串的方法。 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); } 晕。。你这样哪里可以转型成功。 List<Student> list=stuList; for(Student student:list) { bw.write(str); System.out.println(str); }或则for(Student stduent : stuList){//doSomething} 这样写的话,可以在Student里实现toString方法,就可以直接bw.write(student)了,不然打印的会是类的信息哦。 可以将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)); } } 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; } 正向代理获取真实IP 如何使用代码将本地的html文件转换为图片格式 Jpcaptor异常 调用javabean的问题 一个超难的问题,特向各位请教 散分 包引用问题,请高手指教! 一个小问题 新手求教 java中的host class如何翻译?是什么意思呢? JAXP是如何在底层工作的 JAVA 实现 CRC16算法。已有C 和 C# 版本了。仅用JAVA实现即可
不管是什么list总是可以遍历得到数据,for example
List<String> list = new LinkedList<String>();
for(String str :list){
//doSomething
}
如果真要传btye数组,先把list的东西拿出来,然后在调用String.getBytes()方法
我是这样转换的,虽然写出来没报错,但是一运行就报错!是一种转换错误!
这样会不会把我之前的list里面清空了?新手 麻烦指导下
谢谢
//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"));
String[] s=(String[])stuList.toArray(new String[stuList.size()]); //这行代码的强制转换时不需要的。
BufferWriter有write字符串的方法。
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\out.txt"));
List<String> list=stuList;
for(String str:list)
{
bw.write(str);
System.out.println(str);
}
for(Student student:list)
{
bw.write(str);
System.out.println(str);
}或则for(Student stduent : stuList){
//doSomething
}
例:
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));
}
}
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;
}