文件(读写)无法正确检测newLine字符+更多

时间:2014-04-23 12:28:48

标签: java file exception newline

我想在Java中创建一个程序来检查src是否存在(如果不是要抛出FileNoot)
并将src.txt的内容复制到des.txt
并在开始和结束时打印两个文件的大小

输出结果为:

src.txt is in current directory
Before opening files:Size of src.txt:43 Bytes   Size of des.txt:0 Bytes
After closing files:Size of src.txt:43 Bytes    Size of des.txt:0 Bytes

在src.txt将其内容写入des.txt后,des应为 43字节

首先,我想问一下我是否可以通过编写

来省略文件声明
PrintWriter outStream = new PrintWriter(new FileWriter("des.txt")); 

此外,如果我需要close() 文件和信息流

其次,我想问一下如何调整以下开关盒(系统独立换行)
为了在阅读之后添加换行符。


第三,我想在关闭File时询问try / catch块的重要性
非常抱歉这类问题,但在C中没有错误处理(我认为)close()肯定会有效


我很抱歉这些类型的问题,但我是java的初学者

import java.io.*;
public class Main 
{
    public static void main()throws FileNotFoundException
    {

    File src = new File("src.txt");
    if(src.exists())
        System.out.println("src.txt is in current directory");
    else throw new FileNotFoundException("src.txt is not in current directory");

    BufferedReader inStream = null;
    PrintWriter outStream = null;
    try{
        File des = new File("des.txt");
        inStream = new BufferedReader(new FileReader(src));
        outStream = new PrintWriter(new FileWriter(des));

        System.out.print("Before opening files:Size of src.txt:"+src.length()+" Bytes\t");
        System.out.println("Size of des.txt:"+des.length()+" Bytes");
        int c;
        while((c = inStream.read()) != -1){
            switch(c){
                case ' ': outStream.write('@');
                          break;
                case '\r':
                case '\n':outStream.write('\n');
                          outStream.write('\n');
                          break;
                default:outStream.write(c);
            }
        }
        System.out.print("After closing files:Size of src.txt:"+src.length()+" Bytes\t");
        System.out.println("Size of des.txt:"+des.length()+" Bytes");

    } catch(IOException io) {
        System.out.println("Read/Write Error:"+io.toString());
    } finally {
        try{
                if (inStream != null) {
                inStream.close();
                }

                if (outStream != null) {
                outStream.close();
                }
        } catch (IOException io){
            System.out.println("Error while closing Files:"+io.toString());
        }   
    }
} 
}

1 个答案:

答案 0 :(得分:1)

第一个问题 - >我想问一下我是否可以通过编写

来省略文件声明

PrintWriter outStream = new PrintWriter(new FileWriter(“des.txt”));

是的,你可以这样做,因为filewriter带有以下5种方式

FileWriter(File file)
Constructs a FileWriter object given a File object.
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
FileWriter(FileDescriptor fd)
Constructs a FileWriter object associated with a file descriptor.
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
相关问题