本帖最后由 lhao7 于 2010-12-08 13:05:34 编辑

解决方案 »

  1.   

    这个问题要分为几个核心部分:
    1.读写文件package file;import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Properties;public class FileOperation { /**
     * 读取配置文件
     * 
     * @param path
     * @return 配置文件属性值
     * @throws IOException
     */
    public Properties getProperties(String path) throws IOException {
    InputStream inputStream = this.getClass().getClassLoader()
    .getResourceAsStream(path);
    Properties p = new Properties();
    p.load(inputStream);
    return p;
    } /**
     * 读取文件,以List形式存储
     * @param path
     * @return
     * @throws IOException
     */
    public List<String> getReadLine(String path) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(
    new FileInputStream(path)));
    String strLine = "";
    List<String> list = new ArrayList();
    while((strLine = br.readLine())!=null)
    {
    list.add(strLine);
    }
    br.close();
    return list;
    }


    /**
     * 写文件,指定文件路径和文件内容,文件加载在原有文件内容后面
     * @param content
     * @param path
     * @throws IOException 
     */
    public void writeFile(String content,String path) throws IOException{
    FileWriter fw = new FileWriter(path,true);
    fw.write(content,0,content.length());
    fw.write("\r\n");
    fw.flush();   
    fw.close();
    }

    public static void main(String[] args) throws IOException {
    FileOperation file = new FileOperation();
    Properties p = file.getProperties("config.properties");
    System.out.println(p.getProperty("step"));
    System.out.println(p.getProperty("base"));

    System.out.println();
    }
    }这是我在某次实验的代码,估计你能搞得懂。2.就是文件的操作(主要是文件的移动,适用于各种操作系统,你要做的事是修改路径):package file;
    import java.io.File;public class FileMove { /**
     * 功能描述:创建path目录及其父目录<BR>
     * 
     * @param path
     * @author helloc<BR>
     * 时间:2009-10-22 下午05:01:33<BR>
     */
    public void createFolder(String path) {
    try {
    File file = new File(path);
    if (!file.exists()) {
    file.mkdirs();
    }
    } catch (Exception e) {
    System.out.println("创建文件夹出错");
    e.printStackTrace();
    }
    } /**
     * 功能描述:把filename从from文件夹移动到to文件夹<BR>
     * 
     * @param from
     * @param to
     * @author helloc<BR>
     *         时间:2009-10-22 下午05:08:21<BR>
     */
    public void removeFromTo(String from, String to, String filename) {
    createFolder(to);
    File file = new File(from, filename);
    if (file.isFile()) {
    System.out.println("移动" + filename);
    file.renameTo(new File(to, file.getName()));
    } else {
    System.out.println(filename + "不存在");
    }
    } public static void main(String[] args) {
    String from = "e:\\job\\workspace_helloc\\DYFY\\WebRoot\\userfile\\";
    String to = "e:\\job\\workspace_helloc\\DYFY\\WebRoot\\userfile\\200910\\";
    new FileMove().removeFromTo(from, to,
    "0a5da70100af416c869bdbfea10828ff.jpg");
    }
    }这段代码是参考网上的一段代码,你仔细看一看,针对文件操作类做一下功课相信你能轻松搞定你的问题。
      

  2.   


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.Scanner;public class Test {
    public static void main(String[] args) {
    //根据输入的学生人new学生数组
    Scanner input = new Scanner(System.in);
    System.out.println("请输入学生人数:");
    int num = input.nextInt();
    Student[] students = new Student[num];

    //调用输入方法输入学生信息
    read(students);

    //写入信息到文件
    try {
    writeToFile(students);
    } catch (IOException e) {
    System.out.println("写入文件错误!");
    return;
    }

    //复制文件A.txt到B.txt
    try {
    copy();
    } catch (IOException e) {
    System.out.println("复制文件出错!");
    }

    //清空数组,从文件B读入学生信息
    students = new Student[num];
    try {
    readFromFile(students);
    } catch (IOException e) {
    System.out.println("读取文件错误!");
    }

    //打印学生信息
    print(students);
    }

    private static void readFromFile(Student[] students) throws IOException {
    File in = new File("d:\\B.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
    String temp = "";
    temp = br.readLine();
    int num = 0;
    while((null != temp || !"".equals("")) && (num < students.length)) {
    String[] arr = temp.split(";");
    Student stu = new Student();
    String str = "";
    for(int i=0; i<arr.length; i++) {
    str = arr[i];
    int index = str.indexOf(":") + 1;
    if(str.startsWith("姓名"))
    stu.setName(str.substring(index));
    else if(str.startsWith("学号"))
    stu.setNo(str.substring(index));
    else if(str.startsWith("年龄"))
    stu.setAge(Integer.parseInt(str.substring(index)));
    else if(str.startsWith("身份证号")) 
    stu.setId(str.substring(index));
    else if(str.startsWith("成绩:"))
    stu.setScore(Float.parseFloat(str.substring(index)));
    }
    students[num++] = stu;
    temp = br.readLine();
    }
    br.close();
    }

    /**
     * 复制文件"d:\\A.txt"到"d:\\B.txt"
     * @throws IOException 复制文件出错时抛出IO异常
     */
    private static void copy() throws IOException {
    File in = new File("d:\\A.txt");
    File out = new File("d:\\B.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));
    String temp = br.readLine();
    while(null != temp || !"".equals("")) {
    bw.write(temp);
    bw.newLine();//换一行
    temp = br.readLine();
    }
    bw.close();
    br.close();
    }

    /**
     * 将学生信息写入文件"d:\\A.txt",每个学生的信息占一行
     * @param students 要写入文件的学生信息数组
     * @throws IOException 写文件出错时抛出IO异常
     */
    private static void writeToFile(Student[] students) throws IOException {
    File f = new File("d:\\A.txt");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
    for(int i=0; i<students.length; i++) {
    bw.write(students[i].toString());
    bw.newLine();//换一行
    }
    bw.close();
    }

    /**
     * 打印学生信息
     * @param students 要打印的学生数组
     */
    private static void print(Student[] students) {
    for(Student stu : students) {
    System.out.println(stu);
    }
    }

    /**
     * 输入n个学生的信息
     * @param students 保存学生信息的数组
     */
    private static void read(Student[] students) {
    Scanner input = new Scanner(System.in);
    for(int i=0; i<students.length; i++) {
    System.out.println("请输入一个学生的信息,用空格分隔,输入顺序是,学号 姓名 年龄 身份证号 成绩:");
    Student stu = new Student();
    stu.setNo(input.next());
    stu.setName(input.next());
    stu.setAge(input.nextInt());
    stu.setId(input.next());
    stu.setScore(input.nextFloat());
    students[i] = stu;
    }
    }
    }class Student {
    private String no;
    private String name;
    private int age;
    private String id;
    private float score; public String getNo() {
    return no;
    } public void setNo(String no) {
    this.no = no;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String getId() {
    return id;
    } public void setId(String id) {
    this.id = id;
    } public float getScore() {
    return score;
    } public void setScore(float score) {
    this.score = score;
    }

    /**
     * 重写toString方法,方便文件操作和打印学生信息时要用到
     */
    @Override
    public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("学号:" + no + ";姓名:" + name + ";身份证号:" + id + ";成绩:" + score + ";年龄:" + age);
    return sb.toString();
    }
    }
      

  3.   

    public class Student implements Serializable {
    private String no;
    private int age;
    private String id;
    private String name;
    private Map scores;
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getNo() {
    return no;
    }
    public void setNo(String no) {
    this.no = no;
    }
    public Map getScores() {
    return scores;
    }
    public void setScores(Map scores) {
    this.scores = scores;
    }
    public String toString() {
    StringBuffer bs = new StringBuffer();
    bs.append(this.no);
    bs.append(",");
    bs.append(this.name);
    bs.append(",");
    bs.append(this.age);
    bs.append(",");
    bs.append(this.id);
    bs.append(",");
    Set set = this.scores.entrySet();
    for(Iterator it = set.iterator();it.hasNext();){
    Entry entry = (Entry) it.next();
    bs.append(entry.getKey()+"@"+entry.getValue());
    bs.append("#");
    }
    return bs.substring(0, bs.length()-1);
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    ================public class Test{
    public static void main(String[] args) throws Exception{
    Student[] students = new Student[3];
    readData(students);
    writeData(students);
    }
    private static void readData(Student[] students){
    Scanner s = new Scanner(System.in);
    //输入格式 学号,姓名,年龄,身份证号,课程名@分数#课程名@分数;,,,课程名@分数#课程名@分数;,,,课程名@分数#课程名@分数
    //1,name1,20,123456,数学@90#语文@80;2,name2,21,1234567,数学@80#语文@90;3,name3,20,12345678,数学@91#语文@79
    /*while(s.hasNext()){
    String line = s.nextLine();
    System.out.println(line);
    }*/
    String line = s.nextLine();
    String[] student = line.split(";");
    for(int i = 0;i<student.length;i++){
    String[] stu = student[i].split(",");
    Student aStudent = new Student();
    aStudent.setNo(stu[0]);
    aStudent.setName(stu[1]);
    aStudent.setAge(Integer.parseInt(stu[2]));
    aStudent.setId(stu[3]);
    Map scores = new HashMap();
    String[] score = stu[4].split("#");
    for(int j=0;j<score.length;j++){
    String[] keyValue = score[j].split("@");
    scores.put(keyValue[0],keyValue[1]);
    }
    aStudent.setScores(scores);
    students[i] = aStudent;
    }
    }
    private static void writeData(Student[] students) throws IOException, ClassNotFoundException{
    File f = new File("c:\\student.txt");
    if(!f.exists()){
    f.createNewFile();
    }
    OutputStream os = new FileOutputStream(f);
    ObjectOutput oo = new ObjectOutputStream(os);
    oo.writeObject(students);
    oo.close();
    os.close();
    copyAndShow(f);
    }
    private static void copyAndShow(File f) throws IOException, ClassNotFoundException{
    InputStream is = new FileInputStream(f);
    ObjectInput oi = new ObjectInputStream(is);
    File ff = new File("c:\\student1.txt");
    if(!ff.exists()){
    ff.createNewFile();
    }
    OutputStream os = new FileOutputStream(ff);
    ObjectOutput oo = new ObjectOutputStream(os);
    Object obj = oi.readObject();
    oo.writeObject(obj);
    oo.close();
    os.close();
    oi.close();
    is.close();
    is = new FileInputStream(ff);
    oi = new ObjectInputStream(is);
    Student[] students = (Student[]) oi.readObject();
    for(int i = 0;i<students.length;i++){
    Student student = students[i];
    System.out.println(student);
    }
    }}
    代码不是很严谨..........