⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readwritefile.java

📁 自己编写的几个类
💻 JAVA
字号:
package com.zte.cdma.autotest.mulautotest.buildxml;
//package buildxml;

import java.io.*;

/**
 *<p>版权所有(C)2002,深圳市中兴通讯股份有限公司</p>
 *
 *<p>文件名称:VariableAccess.java</p>
 *<p>文件标识:</p>
 *<p>内容摘要:实现对文本文件的读写和操作</p>
 *<p>当前版本://输入当前版本</p>
 *<p>作    者:鄢彪</p>
 *<p>完成日期:输入完成日期,例如:2002-10-11</p>
 *
 *<p>修改记录1:修改历史记录,包括修改日期、修改者及修改内容</p>
 *<pre>
 *	修改日期:
 *	版 本 号:
 *	修 改 人:
 *	修改内容:
 *</pre>
 */

public class ReadWriteFile {
    private String fileName = null; // 保存异常数据的文件名
    private BufferedWriter outFile = null;
    private BufferedReader inFile = null;
    private boolean APPEND = true;
//    private URL fileURL = null;

    public ReadWriteFile(String inFileName) {
        fileName = inFileName;
    }

//    public ReadWriteFile(URL inFileName) {
//        fileURL = inFileName;
//    }

    /**
     * 设置保存异常数据的文件名
     * @param inFileName 输入的文件名
     */
    public void setFileName(String inFileName) {
        fileName = inFileName;
    }

    /**
     * 设置增加文件的模式
     * @param Append 为true时为追加,为false时为重写
     */
    public void setAppend(boolean Append) {
        APPEND = Append;
    }

    /**
     * 获取保存异常数据的文件名
     * @return 保存异常数据的文件名
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * 打开读文件
     */
    public void openReader() throws FileNotFoundException {
        try {
            if (fileName != null) {
                inFile = new BufferedReader(new FileReader(fileName));
            }
        }
        catch (FileNotFoundException ex) {
            throw new FileNotFoundException("打开读文件'" + fileName + "'时发生异常:\n" +
                                            ex.getMessage());
        }
    }

    /**
     * 关闭读文件
     */
    public void closeReader() throws IOException {
        try {
            if (inFile != null) {
                inFile.close();
            }
            inFile = null;
        }
        catch (IOException ex) {
            throw new IOException("关闭读文件时" + fileName + "发生异常:\n" +
                                  ex.getMessage());
        }
    }

    /**
     * 打开写文件
     */
    public void openWriter(boolean append) throws IOException {
        try {
            if (fileName != null) {
                File writeFile = new File(fileName);
                writeFile = new File(writeFile.getAbsolutePath());
                if (!writeFile.getParentFile().exists()) {
                    writeFile.getParentFile().mkdirs();
                }
                outFile = new BufferedWriter(new FileWriter(fileName, append));
                APPEND = append;
            }
        }
        catch (IOException ex) {
            throw new IOException("打开写文件时" + fileName + "发生异常:\n" +
                                  ex.getMessage());
        }
    }

    /**
     * 关闭写文件
     */
    public void closeWriter() throws IOException {
        try {
            if (outFile != null) {
                outFile.close();
            }
            outFile = null;
        }
        catch (IOException ex) {
            throw new IOException("关闭写文件时" + fileName + "发生异常:\n" +
                                  ex.getMessage());
        }
    }

    /**
     * 将传入的字符串写入文件
     * @param inStr 传入的字符串
     */
    public void writeSingleStringToFile(String inStr) throws IOException {
        try {
            if (outFile == null) {
                openWriter(APPEND);
            }
            if(inStr == null)
                return;
            if(outFile == null)
                return;
            outFile.write(inStr);
            outFile.newLine();
            outFile.flush();
            closeWriter();
        }
        catch (IOException ex) {
            throw new IOException("写文件时" + fileName + "发生异常:\n" + ex.getMessage());
        }
    }

    /**
     * 将传入的字符串写入文件
     * @param inStr 传入的字符串
     * @param newLine 是否在其后增加行标记
     */
    public void writeSingleStringToFile(String inStr, boolean newLine) throws
        IOException {
        try {
            if (outFile == null) {
                openWriter(APPEND);
            }
            if(outFile != null) {
                outFile.write(inStr);
                if (newLine) {
                    outFile.newLine();
                }
                outFile.flush();
                closeWriter();
            }
        }
        catch (IOException ex) {
            throw new IOException("写文件时" + fileName + "发生异常:\n" + ex.getMessage());
        }
    }

    /**
     * 将传入的字符串写入文件
     * @param inStr 要写入的字符串
     * @param off 写入的偏移位置
     * @param len 写入的长度
     */
    public void writeSingleStringToFile(String inStr, int off, int len) throws
        IOException {
        try {
            if(inStr != null){
                if (off > 0 && (off + len) < inStr.length() && len >=0) {
                    if (outFile == null) {
                        openWriter(APPEND);
                    }
                    outFile.write(inStr, off, len);
                    outFile.newLine();
                    outFile.flush();
                    closeWriter();
                }
            }
        }
        catch (IOException ex) {
            throw new IOException("写文件时" + fileName + "发生异常:\n" + ex.getMessage());
        }
    }

    /**
     * 在文件尾增加一个跳行符
     */
    public void writeNewLine() throws IOException {
        try {
            if (outFile == null) {
                openWriter(true);
            }
            outFile.newLine();
            outFile.flush();
            closeWriter();
        }
        catch (IOException ex) {
            throw new IOException("写文件时" + fileName + "发生异常:\n" + ex.getMessage());
        }
    }

    /**
     * 从当前位置读取文件中的一行
     * @return 文件中的一行
     */
    public String readSingleStringFromFile() throws IOException {
        try {
            if (inFile == null) {
                openReader();
            }
            String line = inFile.readLine();
            return line;
        }
        catch (EOFException ex) {
            throw new IOException("读文件尾指针时" + fileName + "发生异常:\n" +
                                  ex.getMessage());
        }
        catch (Exception e) {
            throw new IOException("读文件时" + fileName + "发生异常:\n" + e.getMessage());
        }

    }

    /**
     * 读取文件中的所有内容
     * @return 文件中的所有内容
     */
    public String readFileContent() {
        String content = "";
        try {
            if (inFile == null) {
                openReader();
            }
            String lineContent = null;
            while ( (lineContent = inFile.readLine()) != null) {
                content += lineContent + "\n";
            }
            closeReader();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 读取文件中的行数
     * @return 总行数
     */
    public int getLineCount() throws IOException {
        int lineCount = 0;
        try {
            if (inFile == null) {
                openReader();
            }
            if(inFile == null)
                return -1;
            while (inFile.readLine() != null) {
                lineCount++;
            }
            closeReader();
        }
        catch (EOFException ex) {
            throw new IOException("读文件尾指针时" + fileName + "发生异常:\n" +
                                  ex.getMessage());
        }
        catch (java.io.IOException e) {
            throw new IOException("读文件时" + fileName + "发生异常" + e.getMessage());
        }
        return lineCount;
    }

    public void saveAsFile(String inFileName) throws IOException {
        ReadWriteFile newFile = new ReadWriteFile(inFileName);
        newFile.writeSingleStringToFile(this.readFileContent());
    }

    /**
     * 清空文件内容
     */
    public void clearFileContent() throws IOException {
        openWriter(false);
        closeWriter();
        openWriter(true);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -