【问题标题】:File writing in java [duplicate]用java编写文件[重复]
【发布时间】:2014-11-19 14:49:35
【问题描述】:

我刚刚开始我的大学之旅(“耶”)。我也是该网站的新手,所以就提问问题而言,我可能做错了事,请随意向我讲授。

给了我一个项目,该项目已经全部评分,程序应该 ==>> 首先读取标准输入行(使用键盘输入文件名)和每一行输入,如果用户进入exit,应用程序终止;否则,应用程序将该行解释为文本文件的名称。应用程序创建或重新创建此文件并向其写入两行输出,即文件名以及当前日期和时间。然后,应用程序关闭文件,重新打开以供读取,并将其内容写入标准输出。应用程序将用方括号括起来的文件名写入标准输出。写入文件名后, 应用程序写入文件的内容,每一行都以相应的行为前缀 数字、一个完整的冒号和一个空格。 我的措辞和我的教授一样,所以对于任何不清楚的陈述,我深表歉意。这是我得到的:

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class Project1
{
  public static void main() throws IOException
  {       
    String input = "";
    while(!sc.equals("exit"))
    {
        System.out.println("Enter file here!\n Type 'exit' to terminate");
        Scanner sc = new Scanner(System.in);
        input = sc.nextLine();        
        try
        {
            File file = new File (input,".txt"); // Creates pointer to a file.
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            file.createNewFile();
            file.getAbsolutePath();
            printFileAndDate(file);
        }
        catch(IOException e)
        {
            System.out.print("Something wrong :(");
            e.printStackTrace();
        }
    }
  System.exit(0);
  }
  static void printFileAndDate(File temp)
  {   
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     Calendar cal = Calendar.getInstance();
     System.out.println("[ " + temp.getPath() + " ]");
     System.out.println(dateFormat.format(cal.getTime()));
  }        
}

我在那里尝试做的事情如下:

-获取用户输入 => 将输入保存为文件 => 调用方法“printFileAndDate”并以正确的格式打印文件以及当前日期和时间。

但是,每当我运行它时,它总是给我一个异常错误,这意味着该文件从未真正创建过或者它无法找到它。

【问题讨论】:

  • 你能贴出堆栈跟踪吗?发布您的异常错误?
  • 始终在问题中显示确切的错误。
  • 这是要求code review,以后请参阅codereview.stackexchange.com

标签: java


【解决方案1】:

我可以找到 ISSUE 列表:

首先,你的主要方法签名完全错误

public static void main() throws IOException

改成

 public static void main(String[] args) throws IOException 

其次,在main方法中抛出异常并不是一个好习惯。

最好的做法是使用 try catch 块

第三,你在 while 循环之后有你的 Scanner 变量,这是没有意义的

 while(!sc.equals("exit"))
         {
        System.out.println("Enter file here!\n Type 'exit' to terminate");
        Scanner sc = new Scanner(System.in); <-?!!!!!!

改成

   System.out.println("Enter file here!\n Type 'exit' to terminate");
   Scanner sc = new Scanner(System.in); 
   while(!sc.equals("exit"))
         {

第四,你这样定义文件变量

 File file = new File (input,".txt"); <-- totally wrong

改成

File file = new File ("input.txt"); <-- if you use relative path 

第五个main方法的末尾不需要System.exit(0);

【讨论】:

  • 第五,System.exit(0)不用main结尾。
  • @DavidConrad 我同意为什么要投反对票?
  • 我没有给你投反对票。
  • 感谢选民的解释:)
  • @DavidConrad 我不明白为什么会否决完美的解释 :)
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2013-10-27
相关资源
最近更新 更多