簡體   English   中英

在Java中將“.dat”文件轉換為“.text”文件

[英]Converting ".dat" file into ".text" file in Java

我在我的 java 中將“.dat”文件轉換為“.text”文件時遇到了一些困難。 我正在使用 Apache Netbeans。

下面是問題:

Book 類:該類具有以下私有數據成員:

  • 整數年;
  • 字符串標題;

您必須創建適當的 getter 和 setter 方法。 您需要確保此類的任何對象都可以毫無問題地保存到文件中。

FindOldest 類:對於 FindOldest 類,您將假設有一個名為“library.dat”的文件,其中包含多個 Book 對象。 該類將具有從該文件中讀取 Book 對象並將最多 20 個 Book 對象存儲在一個數組中的 main 方法。 之后,主程序將年份小於 2000 的每個 Book 對象的年份和標題寫回名為“oldBooks.txt”的文本文件中。 在生成的文本文件中,每本書的信息將位於不同的行。 您的程序必須具有 IO 異常處理,通過在系統控制台中向程序用戶提供適當的反饋來處理從文件“library.dat”讀取和保存到“oldBooks.txt”時可能出現的問題。

這是代碼:

書籍.java

 package Question2;
 
 public class Book 
 {
     // The private instance variables
     private int year;
     private String title;

     /** Constructs a Book instance with the given author */
     public Book(int year, String title) 
     {
            this.year = year;
            this.title = title;
     }
     
     // Getters and Setters
    /** Returns the year of this book */
    public int getYear() {
       return year;
    }
    
    public int setYear() {
       return year;
    }
    
    /** Returns the year of this book */
    public String getTitle() {
       return title;
    }
    
    public String setTitle() {
       return title;
    }
    
 }

FindOldest.java

 package Question2;
 
 import java.io.FileOutputStream;
 import java.io.FileInputStream;
 import java.io.BufferedOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.util.Properties;
 import java.util.Scanner;
 import java.util.Set;
 
 public class FindOldest {
     
     static int year;
     static String title;
     
     public static void main(String[] args)
     {
         try
         {
             Scanner input = new Scanner( System.in );
             Book test = new Book(year, title);
             
             // Reading data from the same file
             DataInputStream dataIn = new DataInputStream(new FileInputStream("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\Question2\\library.dat"));
             
             //output the data to another file
             DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\oldBooks.txt"));
             
             //attach FileOutputStream to BufferedOutputStream
             BufferedOutputStream bout = new BufferedOutputStream(dataOut,1024);
             System.out.println("Enter text (@ at the end):");
             char ch;
             
             while((ch=(char)dataIn.read())!='@')
             {
                 bout.write(ch);
             }
             //close the file
             bout.close();
         }
         catch(Exception ex)
         {
             System.out.println("ERROR - System Failure! You have entered an invalid value. Please restart");
         }
    
     }      
 }

圖書館.dat

2000 Beast
2001 Harry
2002 Master
2003 Twilight
2004 Moana
2005 Encanto
2006 Despicable
2007 Australia
2008 Gandhi
2009 Vikram
2010 Rose
2011 Love
2012 Bouquet
2013 Valentine
2014 Divorce
2015 Siblings
2016 Comic
2017 Twenty
2018 Guess
2019 Spykids
2020 Godzilla

調試或運行代碼時沒有顯示輸出或文本文件。 但它顯示程序成功構建。

請幫助我,因為我不知道錯誤在哪里!

我會盡量給你一個模板來工作。 所以這不是一個工作程序。

請對 java 的文件 IO 做一些研究(也許看看你的講座?)。

編輯: library.dat 文件似乎是基於行的。 這應該會讓您更輕松(不要搜索空格,而是搜索換行符)。 還要看一下 BufferedReader 類。

public static void main(String[] args)
{
  try
  {
    File library = new File("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\Question2\\library.dat");
    File oldBooks = new File("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\oldBooks.txt");

    // TODO: handle possible file IO exceptions
    // e.g. file not existing, no permissions to access the file, ...

    // Reading data from the same file
    InputStream dataIn = new FileInputStream(library);

    //output the data to another file
    OutputStream dataOut = new FileOutputStream(oldBooks);

    char ch = '';
    StringBuffer data = new StringBuffer();

    while(dataIn.available() > 0)
    {
      ch = dataIn.read();
      if (ch == ' ')
      {
        // We found a space, that means we just read a year or a book title...
        // probably we should do something here.
      }
      else
      {
        // Debug and see whats building up in 'data'
        data.append(ch);
      }
    }
    
    // Close the files
    dataIn.close();
    // Whoops, didn't even use dataOut...
    dataOut.close();
  }
  // Exception has subclasses...
  catch(Exception ex)
  {
    System.out.println("ERROR - failure!");
  }
}      

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM