简体   繁体   English

在Java中将“.dat”文件转换为“.text”文件

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

I am having some difficulties to convert the ".dat" file into ".text" file in my java.我在我的 java 中将“.dat”文件转换为“.text”文件时遇到了一些困难。 I am using Apache Netbeans.我正在使用 Apache Netbeans。

Below is the question:下面是问题:

Book class: This class has the following private data members: Book 类:该类具有以下私有数据成员:

  • int year;整数年;
  • String title;字符串标题;

You have to create the appropriate getter and setter methods.您必须创建适当的 getter 和 setter 方法。 You need to make sure that any object of this class that can be saved without problems into a file.您需要确保此类的任何对象都可以毫无问题地保存到文件中。

FindOldest class: For the FindOldest class, you will assume that there is a file called "library.dat" which contains several Book objects. FindOldest 类:对于 FindOldest 类,您将假设有一个名为“library.dat”的文件,其中包含多个 Book 对象。 The class will have main method that reads Book objects from this file and stores up to 20 of those Book objects in an array.该类将具有从该文件中读取 Book 对象并将最多 20 个 Book 对象存储在一个数组中的 main 方法。 After doing that, the main then writes the year and the title of each Book object whose year is less than 2000 back to a text file called "oldBooks.txt".之后,主程序将年份小于 2000 的每个 Book 对象的年份和标题写回名为“oldBooks.txt”的文本文件中。 In the resulting text file the info of each Book will be on a different line.在生成的文本文件中,每本书的信息将位于不同的行。 Your program must have IO exception handling that handle problems that may come in reading from the file "library.dat" and saving to "oldBooks.txt" by giving appropriate feedback to the program user in the system console.您的程序必须具有 IO 异常处理,通过在系统控制台中向程序用户提供适当的反馈来处理从文件“library.dat”读取和保存到“oldBooks.txt”时可能出现的问题。

Here is the code:这是代码:

books.java书籍.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 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");
         }
    
     }      
 }

library.dat图书馆.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

there is no output or text file shown when i debug or run the code.调试或运行代码时没有显示输出或文本文件。 but it shows program successful build.但它显示程序成功构建。

please help me as i do not know where is the mistake!!!请帮助我,因为我不知道错误在哪里!

I'll try to give you a template to work from.我会尽量给你一个模板来工作。 So this is intentionally not a working program.所以这不是一个工作程序。

Please do some research on file IO with java (maybe see the lectures you've had?).请对 java 的文件 IO 做一些研究(也许看看你的讲座?)。

Edit: the library.dat file seems to be line based.编辑: library.dat 文件似乎是基于行的。 This should make things easier for you (don't search for spaces, but for line breaks instead).这应该会让您更轻松(不要搜索空格,而是搜索换行符)。 Also take a look at the BufferedReader class.还要看一下 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!");
  }
}      

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM