JAVA程序 哪位大神能帮忙用eclipse做两道关于I/O问题 简单的就行
两个java问题,请用eclipse完成(简单的方法就好了,最好使用1.PrintWriterClasstoWriteDatatoaFile,2.ReadingLinef...
两个java问题,请用eclipse完成(简单的方法就好了,最好使用1.PrintWriter Class to Write Data to a File,2. Reading Line from a File with the nextLine Method),谢谢
展开
1个回答
展开全部
//第一题
import java.io.File;
import java.io.RandomAccessFile;
/**
* 2015年10月21日下午2:18:55
*
* @author cs12110 TODO 写入文件和统计对象
*/
class Person {
private String firstName;// 名称
private int age;// 年龄
private int broNum;// 兄弟排名
private int sisNum;// 姐妹排名
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String firstName, int age, int broNum, int sisNum) {
super();
this.firstName = firstName;
this.age = age;
this.broNum = broNum;
this.sisNum = sisNum;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getBroNum() {
return broNum;
}
public void setBroNum(int broNum) {
this.broNum = broNum;
}
public int getSisNum() {
return sisNum;
}
public void setSisNum(int sisNum) {
this.sisNum = sisNum;
}
public String toString() {
return "" + this.firstName + "," + this.age + "," + this.broNum + ","
+ this.sisNum + "\n\r";
}
}
public class Question1 {
private RandomAccessFile accessFile;
/**
* 写入文件存储
*
* @param str
* 写入内容
* @return
*/
public boolean writer(String str) {
try {
File file = new File("D:\\sibings.txt");// 文件存储位置
long fileLen = 0;
if (file.exists()) {// 获取文件大小
fileLen = file.length();
}
accessFile = new RandomAccessFile(file, "rw");
accessFile.seek(fileLen);// 文件末尾处加入内容
accessFile.write(str.getBytes());
accessFile.close();// 关闭文件流
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
Question1 q1 = new Question1();
// 初始化五个对象
Person[] pers = { new Person("p1", 20, 1, 3),
new Person("p2", 30, 4, 0), new Person("p3", 40, 2, 2),
new Person("p4", 40, 0, 0), new Person("p5", 10, 0, 2), };
int onlyBro = 0;// 只有兄弟
int onlySis = 0;// 只有姐妹
int both = 0;// 有兄弟和姐妹
int none = 0;// 独生
for (int index = 0; null != pers && index < pers.length; index++) {
if (pers[index].getBroNum() > 0 && pers[index].getSisNum() == 0) {
onlyBro++;
}
if (pers[index].getBroNum() == 0 && pers[index].getSisNum() > 0) {
onlySis++;
}
if (pers[index].getBroNum() > 0 && pers[index].getSisNum() > 0) {
both++;
}
if (pers[index].getBroNum() == 0 && pers[index].getSisNum() == 0) {
none++;
}
q1.writer(pers[index].toString());
}
System.out.println("------------- END ---------------");
System.out.println("Only have brother: " + onlyBro);
System.out.println("Only have sister: " + onlySis);
System.out.println("Have brother and sister: " + both);
System.out.println("Haven't brother and sister: " + none);
}
}
//第二题
import java.io.File;
import java.io.RandomAccessFile;
public class Question2 {
private RandomAccessFile accessFile;
/**
* 显示用户资料
*
* @param info
*/
public void displayInfos(String info) {
System.out.println();
if (null != info && !info.trim().equals("")) {
String[] infos = info.split(" ");// 切割字符串
if (infos.length == 4) {// 用户格式显示
System.out.println(infos[1] + "\t" + infos[0] + "\t" + infos[2]
+ "\t" + infos[3]);
}
}
}
public void read() {
try {
File file = new File("D:\\contacts.txt");
if (file.exists()) {// 文件存在则进行读取
accessFile = new RandomAccessFile(file, "r");
String tempStr = "";
while (null != (tempStr = accessFile.readLine())) {// 循环读取所有资料
displayInfos(tempStr);
}
accessFile.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
Question2 q2 = new Question2();
System.out.println("FirstName\tLastName\tAge\tEmail");
System.out.println("--------------------------------------------");
q2.read();
}
}
//望采纳,生活艰难
追问
如果不用RandomAccessFile,可以用PrintWriter Class帮我写一个吗,
class Person和class Question1 是分开两个class吗?
80财富值
追答
可以啊,只不过现在没什么时间而已.可能要迟一点,稍微吐槽一下,我提问问题一般都是200财富值的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询