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

📄 filehandler2.java

📁 关于文件处理的类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
*
* @param fileName Name of the file.
* @param searchString The partial string to serach for.
* @returnThe full string of the partial string being
*searched for.
* @see RandomAccessFile
* @see #searchForString
* @since JDK1.3
*/


    public String returnFullString(String fileName, String searchString) {
    // Will search for a partial string matching the BEGINNING of a new line.
    // If the string is found, will return the FULL string
    int i;
    long filePointer;
    boolean stringFound = searchForString( fileName, searchString );
    String possString = "QUACK"; // "False" data...


        if ( stringFound ) {


            try {
            fileName = filePath + fileSlash + fileName;
            RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
            raf.seek( 0 );
            String thisString = new String();


                do {
                thisString = raf.readLine();
                possString = thisString.toString();
                if ( searchString.length() < thisString.length() )
                thisString = thisString.substring( 0, searchString.length() );
                i = searchString.compareToIgnoreCase( thisString );
                if ( i == 0 )
                break;
            } while ( raf.getFilePointer() < raf.length() );
            raf.close();
        }


            catch (IOException e) {
            System.out.println("Error opening file: " + e);
        }
    }
    return possString;
} // ends returnFullString
/**
* Returns the end char of a line in the file. Will accept partial
* string as a parameter. For example "abc" in a file with the line
* "abcdefghi" will return the char 'i'.
*
* @param fileName Name of the file.
* @param searchString The user's password being authenticated.
* @returnThe last char in the line being searched for.
* @see RandomAccessFile
* @see#searchForString
* @sinceJDK1.3
*/


    public char retrieveEndLineChar(String fileName, String searchString) {
    // Retrieves the end character of a line...
    int i;
    char c = '9';
    boolean stringFound = searchForString( fileName, searchString );


        if ( stringFound ) {


            try {
            fileName = filePath + fileSlash + fileName;
            RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
            raf.seek( 0 );
            String thisString = new String();
            String thisSubString = new String();


                do {
                thisString = raf.readLine();
                thisSubString = thisString.toString();
                thisSubString = thisSubString.substring( 0, searchString.length() );
                i = searchString.compareToIgnoreCase( thisSubString );
                if ( i == 0 )
                c = thisString.charAt( thisString.length() - 1 );
            } while ( raf.getFilePointer() < raf.length() );
            raf.close();
        }


            catch (IOException e) {
            System.out.println("Error opening file: " + e);
        }
    }
    return c;
} // ends retriveEndLineChar
/**
*
*
* @param fileName Name of the file to insert the line into.
* @param dataToInsert Data to insert.
* @see RandomAccessFile
* @sinceJDK1.3
*/


    public void writeIntoFile (String fileName, String dataToInsert) {
    // Appends a string to the end of the file.
    // Assumes the last character is a line break.
    int i = 360;
    long filePointer;
    boolean iReachesZero = false;
    fileName = filePath + fileSlash + fileName;


        try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
        filePointer = raf.length();
        raf.seek( filePointer );
        raf.writeBytes( dataToInsert + "\n"); // inserts the data plus a line break
        raf.close();
    }


        catch (IOException e) {
        System.out.println("Error opening file: " + e);
    }
} // ends writeIntoFile
/**
* Inserts a line into the file in alphabetical order.
*
* @param fileName Name of the file to insert the line into.
* @param dataToInsert Data to insert.
* @see RandomAccessFile
* @sinceJDK1.3
*/


    public void insertIntoFile(String fileName, String dataToInsert) {
    // Inserts a string into the file in alphabetical order.
    int i = 360;
    long filePointer;
    boolean iReachesZero = false;
    fileName = filePath + fileSlash + fileName;


        try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
        raf.seek( 0 );
        filePointer = 0;
        String thisString = new String();


            do {


                if ( raf.getFilePointer() < raf.length() ) {
                thisString = raf.readLine();
                i = dataToInsert.compareToIgnoreCase( thisString );
            }
            if ( i == 0 )
            iReachesZero = true;
            if ( i > 0 )
            filePointer = raf.getFilePointer();
        } while ( raf.getFilePointer() < raf.length() );


            if ( !iReachesZero ) {
            raf.seek( filePointer );
            Long lengthPos = new Long( raf.length() - raf.getFilePointer() );
            byte[] b;
            b = new byte[ lengthPos.intValue() ];
            raf.readFully(b);
            raf.seek( filePointer );
            raf.writeBytes( dataToInsert + "\n"); // inserts the data plus a line break
            raf.write(b);
        }
        raf.close();
    }


        catch (IOException e) {
        System.out.println("Error opening file: " + e);
    }
} // Ends insertIntoFile
/**
* Deletes a line from the file. Will only accept full
* lines (not partial lines) or the whole thing will
* be screwed up.
*
* @param fileName Name of the file.
* @param dataToDelete Data string to be deleted.
* @see RandomAccessFile
* @see#searchForString
* @sinceJDK1.3
*/


    public void deleteFromFile(String fileName, String dataToDelete) {
    int i = 360;
    long filePointer;
    long filePointer2;
    boolean iReachesZero = false;


        if ( searchForString( fileName, dataToDelete ) ) {


            try {
            fileName = filePath + fileSlash + fileName;
            RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
            raf.seek( 0 );
            filePointer = 0;
            filePointer2 = 0;
            String thisString = new String();


                do {


                    if ( raf.getFilePointer() < raf.length() ) {
                    thisString = raf.readLine();
                    i = dataToDelete.compareToIgnoreCase( thisString );
                }


                    if ( i == 0 ) {
                    iReachesZero = true;
                    filePointer2 = raf.getFilePointer();
                }
                if ( i > 0 )
                filePointer = raf.getFilePointer();
            } while ( raf.getFilePointer() < raf.length() );


                if ( iReachesZero ) {
                raf.seek( filePointer2 );
                Long lengthPos = new Long( raf.length() - filePointer2 );
                long newLength = raf.length() - filePointer2 + filePointer;
                byte[] b;
                b = new byte[ lengthPos.intValue() ];
                raf.readFully(b);
                raf.seek( filePointer );
                raf.write(b);
                raf.setLength( newLength );
            }
            raf.close();
        }


            catch (IOException e) {
            System.out.println("Error opening file: " + e);
        }
    }
} // Ends deleteFromFile
/**
* Returns the nth line in a file in String format.
*
* @param fileName File to browse in.
* @param whichLine The line number to lift the string from.
* @returnThe line contents in String format.
* @seeRandomAccessFile
* @sinceJDK1.3
*/


    public String getLine( String fileName, int whichLine ) {
    int lineCount = 0;
    String thisString = "";
    fileName = filePath + fileSlash + fileName;


        try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
        raf.seek( 0 );


            if ( raf.length() > 0 ) {


                do {
                lineCount++;
                thisString = raf.readLine();
            } while ( ( raf.getFilePointer() < raf.length() ) && ( lineCount < whichLine ) );
        }
        raf.close();
    }


        catch (IOException e) {
        System.out.println("Error opening file: " + e);
    }
    return thisString;
} // Ends getLine
// Directory where you want to keep your files in.
private String filePath = "C:\\jakarta-tomcat-3.2.3\\webapps\\ROOT";
// The "slash" in file organization in your computer...
// WINDOWS USERS: Should stay "\\".
// LINUX USERS: Should amend to "/".
private String fileSlash = "\\";
} // Ends UniversalFileHandler class
// Written by Jennica Humphrey 
// (emelia2002@hotmail.com)
// Free for redistribution as long as you keep the credits and comments unchanged!
		

⌨️ 快捷键说明

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