简体   繁体   English

有人可以告诉我这个Java类做什么吗? I / O相关

[英]Can someone tell me what this Java class does? I/O related

I am relatively new to Java and trying to learn the I/O syntax. 我是Java的新手,尝试学习I / O语法。 Could someone give me a general overview of what this code does? 有人可以概述一下此代码的作用吗? Thanks!! 谢谢!!

import java.io.*;

public class FileReader {
    private String openFile="";
    private String saveFile="";

FileReader(openFile, saveFile)
{
    this.openFile=openFile;
    this.saveFile=saveFile;
}



public String process(){
 System.out.println(this.openFile);
 System.out.println(this.saveFile);
 BufferedReader open=null;
 FileReader openFR=null;
 FileWriter save=null;
 int counter=0;

 String output="";

 if(openFile.equals("")){
     return "No open file specifified\n";
 }
 if(this.saveFile.equals("")){
    return "No save file specified\n";
 }
 try {
     openFR = new FileReader(this.openFile);
     open = new BufferedReader(openFR);

 } catch (FileNotFoundException e) {
     return ("Open file no longer exists\n");
 }
 try {
     save = new FileWriter(saveFile);
 }
 catch (IOException e){
     return ("Error saving the file\n");
 }


 try{
     String temp = open.readLine();
     while(temp != null){
         temp = open.readLine();
         counter++;
         save.write(output + "\n");
     }
 } catch (IOException e){
     e.getStackTrace();
     return ("Error reading open file");
 }

 try {
     save.flush();
 } catch (IOException e) {
     e.printStackTrace();
     return ("Error writing save file");
 }

 return "Operation completed successfully";

 }
}

This is a nice example of how not to code! 这是一个很好的例子,说明如何编码!

Some of the issues: 一些问题:

  • Doesn't close the streams! 不关闭流!
    This could lead to problems due to locked files and/or uncomplete written files 由于锁定的文件和/或不完整的书面文件,这可能导致问题
  • Doesn't use Exceptions or return states to indicate errors. 不使用异常或返回状态来指示错误。
    If you want to know if the operation succeded, you'll have to compare the returned Strings.. If anyone changes the String the depending app won't run anymore. 如果您想知道操作是否成功,则必须比较返回的Strings。如果有人更改了String,则依赖的应用程序将不再运行。
  • member variables only set in constructor should be final 仅在构造函数中设置的成员变量应为最终变量
    So they can't be accidentially assigned. 因此,它们不会被意外分配。
  • local variables should be declared when needed 局部变量应在需要时声明
    declaring them at the start of the method is a relict of older languages 在方法开始时声明它们是旧语言的遗迹
  • ambiguous Exceptions aren't exposed 不公开模棱两可的异常
    If an exception occures, you'll never know what it was, you will just see "Error reading open file" 如果发生异常,您将永远不知道它是什么,您只会看到“读取打开的文件时出错”

Writes the counter number of new lines to the saveFile, because output is always "" . 因为output始终为"" ,所以将新行的counter编号写入saveFile。 I guess it soposed to copy one file to another, but there should be save.write(temp + "\\n"); 我猜它可能会将一个文件复制到另一个文件,但是应该有save.write(temp + "\\n"); to do so. 这样做。

It opens two files, then (if both files exists and can be opened), reads lines from the first file and writes empty lines into the second file in an endless loop until it gets to the end of the first file. 它打开两个文件,然后(如果两个文件都存在并且可以打开),从第一个文件读取行,并 以无穷循环 将空行写入第二个文件 直到到达第一个文件的末尾。

It does not close any of the files btw. 它不会关闭任何文件。

它尝试复制文本文件,但似乎有一些错误。

Here is a link to the Java I/O tutorial. 是Java I / O教程的链接。 If you're trying to get to grips with Java I/O this is the best starting point. 如果您想使用Java I / O,那么这是最好的起点。

The code you've pasted attempts to copy the source file to a new destination file one line at a time but it contains bugs which will prevent this from happening, namely: 您粘贴的代码尝试一次将源文件复制到一个新的目标文件中,但是其中包含的错误会阻止这种情况的发生,即:

  • The first line of the file is read and immediately discarded. 读取文件的第一行,并立即将其丢弃。
  • Each line read is never assigned to "output" and is therefore never written to the destination file. 读取的每一行都不会分配给“输出”,因此也永远不会写入目标文件。 The only thing that is written is "\\n". 唯一被写入的是“ \\ n”。

For me, this code does absolutely nothing except throw a lot of compile time errors. 对我来说,这段代码除了抛出很多编译时错误外,什么也不做。

First of all your class name FileReader clashes with java.io.FileReader, so your call to 首先,您的类名FileReader与java.io.FileReader发生冲突,因此您对

new FileReader(this.openFile);

is actually trying to instantiate an instance of YOUR class FileReader (for which there is no constructor that takes a single string) rather than java.io.FileReader 实际上是在尝试实例化您的类FileReader的实例(对于该实例,没有构造函数采用单个字符串),而不是实例化java.io.FileReader

As well as this openFR is declared to be of type FileReader (your FileReader not java.io.FileReader) so the call to 以及此openFR声明为FileReader类型(您的FileReader不是java.io.FileReader),因此对

open = new BufferedReader(openFR);

also fails because BufferedReader expects a java.io.FileReader object. 也会失败,因为BufferedReader需要一个java.io.FileReader对象。

Your constructor for FileReader doesn't declare the types of the arguments: 您的FileReader构造函数未声明参数的类型:

FileReader(openFile, saveFile) {
    this.openFile=openFile;
    this.saveFile=saveFile;
}

should be: 应该:

FileReader(String openFile, String saveFile) {
    this.openFile = openFile;
    this.saveFile = saveFile;
}

So on to what the program does once these errors are fixed: 因此,解决这些错误后程序将如何处理:

It tries to copy a file by reading all lines from openFile and writing them to saveFile. 它尝试通过从openFile中读取所有行并将它们写入saveFile来复制文件。

What it actually does is: 它的实际作用是:

Reads the first line from the file and discards it. 从文件中读取第一行并将其丢弃。 Enters a loop where it continues to read lines and increment a counter (which is also wrong since it never includes the first line in the count, unless the aim is to ignore the first line???). 进入一个循环,在该循环中它将继续读取行并增加计数器(这也是错误的,因为它永远不会在计数中包括第一行,除非目标是忽略第一行???)。 It then writes a blank line to the saveFile for every line in the openFile (minus the first line). 然后,它为openFile中的每一行(减去第一行)将空白行写入saveFile。 You are also never doing anything with the counter value, so what is the point in counting it? 您也永远不会对计数器值做任何事情,那么对它进行计数有什么意义呢?

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

相关问题 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? 有人能告诉我这段代码(Java)发生了什么吗? - Can someone tell me what is happening in this code (Java)? 膨胀 class android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么 - Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing 有人可以向我解释一下哨兵在Java中的作用吗? 或者它是如何工作的? - Can someone explain to me what a sentinel does in Java? Or how it works? 有人能告诉我我做错了什么吗? 通过LinkedList计数和循环 - Can someone tell me what i'm doing wrong? Counting and looping through LinkedList 有人可以告诉我for循环有什么问题吗 - Can someone please tell me what is wrong with the for loop 使用Android Studio的人可以告诉我该怎么做吗? - Can someone who uses android studio tell me what to do? 有人可以告诉我for循环怎么了吗? - Can someone tell me what's wrong with the for loops? 有人可以告诉我我在Java中使用swing的错误吗? - Can someone tell me my error with swing in java? 我需要有人告诉我代码中的逻辑出了什么问题 - I need someone to tell me what is wrong with logic in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM