日期:2014-05-20 浏览次数:20909 次
1 四朝政治风云 311 115 /丛书/走向未来丛书/1988年出版 怀效锋 NULL
2 人的发现:马丁.路德与宗教改革 228 111 /丛书/走向未来丛书/1984年出版 李平晔 NULL
//记录书籍信息的类
private class BookInformation
{
public int BookID; //书籍ID
public String BookName; //书籍名称
public int BookPage; //书籍页数
public int TypeID; //类型ID
public String TypeName; //类型名称
public String Author; //作者姓名
public String Translater; //译者姓名
}
//输出调试信息
private void debug(String msg, int i)
{
System.out.print(msg + "--->");
System.out.println(i);
}
//从文件插入所有图书信息
private void InsertAll()throws Exception
{
// TODO Auto-generated method stub
FileReader fr = null;
Scanner scanner = null;
try
{
System.out.println("请输入含有要导入的书籍信息的文件的路径和文件名,用/作为分隔符:");
String fileName = null;
scanner = new Scanner(System.in);
fileName = scanner.next();
fr = new FileReader(fileName);
//将文件内容读入字符串
char[] cbuf = new char[1024];
int hasRead = 0;
String[] bookInfoStr = null;
StringBuilder InfoStr = new StringBuilder();
while ((hasRead = fr.read(cbuf)) > 0)
{
String str = new String(cbuf, 0, hasRead);
InfoStr.append(str);
}
bookInfoStr = InfoStr.toString().split(" ");
//将内容输入数据库
int i = 0;
System.out.println(bookInfoStr.length);
BookInformation bookinfo = new BookInformation();
while (i < bookInfoStr.length)
{
debug(bookInfoStr[i], i);
bookinfo.BookID = Integer.parseInt(bookInfoStr[i]);
i++;
debug(bookInfoStr[i], i);
bookinfo.BookName = bookInfoStr[i];
i++;
debug(bookInfoStr[i], i);
bookinfo.BookPage = Integer.parseInt(bookInfoStr[i]);
i++;
debug(bookInfoStr[i], i);
bookinfo.TypeID = Integer.parseInt(bookInfoStr[i]);
i++;
debug(bookInfoStr[i], i);
bookinfo.TypeName = bookInfoStr[i];
i++;
debug(bookInfoStr[i], i);
bookinfo.Author = bookInfoStr[i];
i++;
debug(bookInfoStr[i], i);
bookinfo.Translater = bookInfoStr[i];
i++;
}
}
finally
{
if (fr != null)
{
fr.close();
}
if (scanner != null)
{
scanner.close();
}
}
}
请输入含有要导入的书籍信息的文件的路径和文件名,用/作为分隔符:
./test.txt
14
1--->0
四朝政治风云--->1
311--->2
115--->3
/丛书/走向未来丛书/1988年出版--->4
怀效锋--->5
NULL--->6
2--->7
Exception in thread "main" java.lang.NumberFormatException: For input string: "