前景:
是这样的
现在有个
public class Book{
private String bookname;//书籍的名字
private String duthor;//书的作者
private String isbn;//书的ISBN
private double price;//书的价额
private String booktype;//书的类型
private String published;//出版社名字
private int bookid;
public int count;
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getDuthor() {
return duthor;
}
public void setDuthor(String duthor) {
this.duthor = duthor;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getPublished() {
return published;
}
public void setPublished(String published) {
this.published = published;
}
}现在把Book类添加到List进去import java.util.List;public class addBook{
public static void addU(List list,Object o){
list.add(o);
}
}
现在就是添加到List后要保存到文件进去,这边要怎么写代码呢?然后每次还可以读取
感觉有点蒙希望大家帮忙
是这样的
现在有个
public class Book{
private String bookname;//书籍的名字
private String duthor;//书的作者
private String isbn;//书的ISBN
private double price;//书的价额
private String booktype;//书的类型
private String published;//出版社名字
private int bookid;
public int count;
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getDuthor() {
return duthor;
}
public void setDuthor(String duthor) {
this.duthor = duthor;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getPublished() {
return published;
}
public void setPublished(String published) {
this.published = published;
}
}现在把Book类添加到List进去import java.util.List;public class addBook{
public static void addU(List list,Object o){
list.add(o);
}
}
现在就是添加到List后要保存到文件进去,这边要怎么写代码呢?然后每次还可以读取
感觉有点蒙希望大家帮忙
解决方案 »
- 从服务器上下载Excel,弹出保存和打开按钮后页面就死了,各位大侠看看小弟的代码是否有误。
- 急! rs.updateRow批量更新问题
- 我现在有一个问题,视图应该直接从模型读取数据,还是通过控制器读取传来的数据?MVC太容易让人迷惑了,我看网上说的理解都不一样。
- Internet Explorer 末日
- spring中的访问Hibernate的问题请教!
- 求救!Io 异常: End of TNS data channel
- Hibernate+tomcat的問題
- 初学者所碰到的javamail问题~~~~~~~~
- 关于j2ee的几个问题
- 关于启动tomcat时报错,请大家帮我看看,谢谢!
- dwr input属性扩展
- 字符串 utf-8 输出到文件
package test;public class Book {
private String bookname; private String duthor; private String isbn; private double price; private String booktype; private String published; private int bookid; public int count; public String toString() {
return this.bookname + ";" + this.duthor + ";" + this.isbn + ";"
+ this.price + ";" + this.booktype + ";" + this.published + ";"
+ this.bookid + ";" + this.count;
} public String getBookname() {
return bookname;
} public void setBookname(String bookname) {
this.bookname = bookname;
} public String getDuthor() {
return duthor;
} public void setDuthor(String duthor) {
this.duthor = duthor;
} public String getIsbn() {
return isbn;
} public void setIsbn(String isbn) {
this.isbn = isbn;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public String getBooktype() {
return booktype;
} public void setBooktype(String booktype) {
this.booktype = booktype;
} public String getPublished() {
return published;
} public void setPublished(String published) {
this.published = published;
} public int getBookid() {
return bookid;
} public void setBookid(int bookid) {
this.bookid = bookid;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
}}
BookUtil.java
package test;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;public class BookUtil {
public static ArrayList<Book> readFile(String fileName) throws Exception {
ArrayList<Book> lst = null;
String s = null;
File f = new File(fileName); try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
// start line 5 test step list
lst = new ArrayList<Book>();
while ((s = br.readLine()) != null) {
String[] line = s.split("\0");
Book b = new Book();
b.setBookname(line[0]);
b.setDuthor(line[1]);
b.setIsbn(line[2]);
b.setPrice(Double.valueOf(line[3]));
b.setBooktype(line[4]);
b.setPublished(line[5]);
b.setBookid(Integer.parseInt(line[6]));
b.setCount(Integer.parseInt(line[7]));
lst.add(b);
}
System.out.println("read file finished");
} catch (Exception e) {
throw e;
}
return lst;
} public static void saveFile(String fileName, ArrayList<Book> lst)
throws Exception {
try {
FileWriter fw = new FileWriter(fileName);
String content = "";
for (int i = 0; i < lst.size(); i++) {
Book b = (Book) lst.get(i);
String line = b.getBookname() + "\0" + b.getDuthor() + "\0"
+ b.getIsbn() + "\0" + String.valueOf(b.getPrice())
+ "\0" + b.getBooktype() + "\0" + b.getPublished()
+ "\0" + b.getBookid() + "\0" + b.getCount() + "\n";
content += line; }
fw.write(content);
fw.flush();
fw.close();
System.out.println("save file finished");
} catch (Exception e) {
throw e;
}
} public static void main(String[] args) {
try {
ArrayList<Book> lst = new ArrayList<Book>();
int i = 0;
while (i++ < 5) {
Book b = new Book();
b.setBookname("line[0]" + i);
b.setDuthor("line[1]" + i);
b.setIsbn("line[2]" + i);
b.setPrice(1.33);
b.setBooktype("b[4]" + i);
b.setPublished("b[5]" + i);
b.setBookid(i + 1);
b.setCount(i + 1);
lst.add(b);
}
String fileName = "E:\\result.dat";
BookUtil.saveFile(fileName, lst);
ArrayList lst1 = BookUtil.readFile(fileName);
for (int j = 0; j < lst1.size(); j++) {
System.out.println(lst1.get(j).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}}