JAVA将字符串读入到文本文件
发布网友
发布时间:2022-05-11 00:03
我来回答
共2个回答
热心网友
时间:2023-11-18 00:44
完全复制给你:
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Library {
ArrayList<Book> bookList = new ArrayList<Book>();
Scanner sc;
public void addBook() {
System.out.println("请输入新图书的信息");
System.out.println("例如:书名#ISBN#作者#出版社#年份");
Scanner sc2 = new Scanner(System.in);
try {
String str2 = sc2.nextLine();
String[] information = str2.split("#");
Book newBook = new Book(information[0], information[1],
information[2], information[3], information[4]);
bookList.add(newBook);
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
e.printStackTrace();
addBook();
}
while (true) {
try {
System.out.print("是否继续输入(y/n): ");
String sin = sc2.nextLine();
System.out.println();
if ((sin.equals("y")) || (sin.equals("Y"))) {
addBook();
} else {
String fileName = "e:/test.txt";//这部分是新加的内容,写入的文件路径及名称
writeFile(fileName, bookList);//进行写入,下面有实现
}
break;
} catch (Exception e) {
System.out.println("错误,请重新输入!");
}
}
}
//实现由bookList到文件的追加写入过程~~
public void writeFile(String fileName, ArrayList<Book> bookList)
throws IOException {
Iterator<Book> it = bookList.iterator();
StringBuilder sb = null;
FileWriter writer = new FileWriter(fileName, true);
while (it.hasNext()) {
Book b = it.next();
sb = new StringBuilder();
sb.append("书名:" + b.getName() + " ISBN:" + b.getISBN() + " 作者:"
+ b.getAuthor() + " 出版社:" + b.getPress() + " 年份:"
+ b.getYearOfPublication());
System.out.println(sb.toString());
writer.write(sb.toString());
}
writer.close();
}
public static void main(String[] args) throws IOException {
Library l = new Library();
l.addBook();
}
}
其中Book类中对所有属性添加了getter方法,详细看下面的代码~~
public class Book {
private String ISBN;
private String serialNumber; // 图书编号
private String name; // 书名
private String author; // 作者
private String press; // 出版社
private String yearOfPublication; // 年份
private String borrowerID; // 借阅者ID
private String timeOfBorrowing; // 借阅时间
private String timeOfReturn; // 归还日期
private boolean isRequired; // 是否请求
public Book(String name, String ISBN, String author, String press,
String yearOfPublication) {
this.name = name;
this.ISBN = ISBN;
this.author = author;
this.press = press;
this.yearOfPublication = yearOfPublication;
}
public String getISBN() {
return ISBN;
}
public String getSerialNumber() {
return serialNumber;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPress() {
return press;
}
public String getYearOfPublication() {
return yearOfPublication;
}
public String getBorrowerID() {
return borrowerID;
}
public String getTimeOfBorrowing() {
return timeOfBorrowing;
}
public String getTimeOfReturn() {
return timeOfReturn;
}
public boolean isRequired() {
return isRequired;
}
}
望满意~~
热心网友
时间:2023-11-18 00:44
这用到了流,给你一个例子,你试试,在找找io,
从后台控制传图片
public static void main(String args[]) {
try{
//定义输入文件流
FileInputStream in = new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹\\mediaplayer-5.9-viral\\20120310.flv");
//将文件输入流构造到缓存
BufferedInputStream bin = new BufferedInputStream(in);
//定义输出文件流
File outputFile = new File("C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹\\20120310.flv");//定义拷贝目标文件
FileOutputStream out = new FileOutputStream(outputFile);
//将输出文件流构造到缓存
BufferedOutputStream bout = new BufferedOutputStream(out);
System.out.println("--------------------1111");
int c;
//循环读取文件和写入文件
while ((c = bin.read()) != -1){
bout.write(c);
bout.flush();}
//关闭输入输出流,释放资源
System.out.println("-----------------------12222");
bin.close();
bout.close();
}catch(Exception e){
e.printStackTrace();
}
}