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

📄 program5.txt

📁 Java多线程机制的源码包括线程控制方法、多线程实现方法 、如何用接口来创建线程、输入输出流类、创建目录和删除文件
💻 TXT
📖 第 1 页 / 共 4 页
字号:
运行:java FileTest1 \wjjava\filetest1
运行结果:
----------------------------- 
Absolute Path:   c:\wjjava\filetest1 
File Path:           \wjjava\filetest1
File Name:         filetest1
Parent Dirtory:   \wjjava
-----------------------------
Readable:        Yes 
Writable:          Yes
Is directoty:      Yes
Is file:               No
Is absolute path:No

—————
运行:  java FileTest1 \wjjava\filetest1\filetest1.java
运行结果:
----------------------------- 
Absolute Path:   c:\wjjava\filetest1\filetest1.java 
File Path:           \wjjava\filetest1\filetest1.java
File Name:         filetest1.java
Parent Dirtory:   \wjjava\filetest1
-----------------------------
Readable:        Yes 
Writable:          Yes
Is directoty:      No
Is file:               Yes
Is absolute path:No

—————
运行:java FileTest1 \wjjava
运行结果:
----------------------------- 
Absolute Path:   c:\wjjava
File Path:           \wjjava
File Name:         wjjava
Parent Dirtory:   \
-----------------------------
Readable:        Yes 
Writable:          Yes
Is directoty:      Yes
Is file:               No
Is absolute path:No

—————
运行:java FileTest1 \wjjava
运行结果:
----------------------------- 
Absolute Path:   c:\
File Path:           \
File Name:       
Parent Dirtory:   null
-----------------------------
Readable:        Yes 
Writable:          Yes
Is directoty:      Yes
Is file:               No
Is absolute path:No


————————————————————————————
————————————————————————————

2.创建目录和删除文件

例2:
//Program Name: FileTest2.java
import java.io.*;
class FileTest2 
{
    public static void main(String[] args) 
    {
        File dir, subdir;
        if(args.length != 1) 
        {
            System.err.println("Usage: java FileTest2 <newDirPath>");
            System.exit(-1);
        }
        dir = new File(args[0]);
        if(dir.exists()) 
            System.out.println(dir.getPath() + " already exist!");
        else 
        {    
            if(dir.mkdirs()) 
            {
                System.out.println("Created directory:    " + dir.getAbsolutePath());
                subdir = new File(dir, "newSub");
                if (subdir.mkdir()) 
                {
                    System.out.println("Created subdirectory: " + subdir.getAbsolutePath());
                    subdir.delete();  //删除才创建的目录
                }
                else
                    System.out.println("Could not create subdirectory " + subdir.getAbsolutePath());
                dir.delete();
            } 
            else 
                System.out.println("Could not create directory " +    dir.getAbsolutePath());
        }
    }
}
 
————————————————————————————
————————————————————————————

3.文件更名

//Program Name: FileRename.java
import java.io.*;
class FileRename 
{
    public static void main(String[] args) 
    {
        String message;
        if (args.length != 2) 
        {
            System.err.println("Usage: java FileRename <file1> <file2>");
            System.exit(-1);
        }
        File f1 = new File(args[0]);
        File f2 = new File(args[1]);
        if (f1.equals(f2)) 
        {
            System.err.println("Cannot rename a file to itself");
            System.exit(-1);
        }
        message = f1.renameTo(f2) ? " renamed to " : " could not be renamed to ";
        System.out.println(f1.getPath() + message + f2.getPath());
    }
}

————————————————————————
————————————————————————

4.目录清单

//Program Name: FileDir.java
import java.io.File;
import java.util.Date;
class FileDir 
{
    public static void main(String[] args) 
    {
        if (args.length != 1) 
        {
            System.err.println("Usage: java FileDir filepath");
            System.exit(-1);
        }
        File f = new File(args[0]);
        String[] ls = f.list();
        for (int i = 0; ls != null && i <ls.length; i++) 
            printOne(new File(f, ls[i])); 
    } 
    public static void printOne(File f) 
    { 
        if (f.exists()) 
        {
            System.out.print(f.canRead() ? "r" : "-");
            System.out.print(f.canWrite() ? "w" : "-"); 
            System.out.print(f.isDirectory() ? "d" : "-"); 
            if(!f.isDirectory()) 
                System.out.print("\t\t" + f.length()); 
            else 
                System.out.print("\t<Dir>\t");
            System.out.print('\t');
            System.out.print(new Date(f.lastModified()));
            System.out.print('\t');
        } 
        else 
        {
            System.out.print("\t\t\t\t\t");
        }
            System.out.println(f.getName());
    }
}


运行:java FileDir \wjjava\filetestc
运行结果:
rw-        1130    Tue Oct 26 16:30:23 CST 2001  FileTest.java
rw-        1492    Tue Oct 26 16:30:30 CST 2001  FileTest.class

运行:java FileDir \wjjava\
运行结果:
rwd   <dir>          Tue Oct 26 16:34:33 CST 2001  Computer
rw-          1203    Tue Oct 26 16:30:23 CST 2001  Hello.java
rw-          1345    Tue Oct 26 16:30:30 CST 2001  Hello.class
rwd   <dir>          Tue Oct 26 16:30:23 CST 2001  UsePhoneCard
rwd   <dir>          Tue Oct 26 16:30:23 CST 2001  MultiPara

——————————————————————————————
——————————————————————————————

4.5.2读写文件

RandomAccessFile类,它的定义如下:
public class RandomAccessFile implements DataOutput, DataInput 
{
    public RandomAccessFile(String name, String mode) throws IOException;
    public RandomAccessFile(File file, String mode) throws IOException;

    //misc methods
    public final FileDescriptor getFD() throws IOException;
    public long getFilePointer() throws IOException;
    public void seek(long pos) throws IOException;
    public long length() throws IOException;
    public void close() throws IOException;

    public int read() throws IOException;
    public int read(byte b[], int off, int len) throws IOException;
    public int read(byte b[]) throws IOException;

    //DataInput interface
    public final void readFully(byte b[]) throws IOException;
    public final void readFully(byte b[], int off, int len) throws IOException;
    public int skipBytes(int n) throws IOException;
    public final boolean readBoolean() throws IOException;
    public final byte readByte() throws IOException;
    public final int readUnsignedByte() throws IOException;
    public final short readShort() throws IOException;
    public final int readUnsignedShort() throws IOException;
    public final char readChar() throws IOException;
    public final int readInt() throws IOException;
    public final long readLong() throws IOException;
    public final float readFloat() throws IOException;
    public final double readDouble() throws IOException;
    public final String readLine() throws IOException;
    public final String readUTF() throws IOException;

    //DataOutput interface
    public void write(int b) throws IOException;
    public void write(byte b[]) throws IOException;
    public void write(byte b[], int off, int len) throws IOException;
    public final void writeBoolean(boolean v) throws IOException;
    public final void writeByte(int v) throws IOException;
    public final void writeShort(int v) throws IOException;
    public final void writeChar(int v) throws IOException;
    public final void writeInt(int v) throws IOException;
    public final void writeLong(long v) throws IOException;
    public final void writeFloat(float v) throws IOException;
    public final void writeDouble(double v) throws IOException;
    public final void writeBytes(String s) throws IOException;
    public final void writeChars(String s) throws IOException;
    public final void writeUTF(String str) throws IOException;
}

————————————————————————————

例1:下面的程序说明向文件中写出数据的方法

 //Program Name: RandomTest.java
import java.io.*;
class RandomTest 
{
    public static void main(String[] args) 
    {
        RandomAccessFile raf = null;
        if (args.length != 1) 
        {
            System.err.println("Usage: java RandomTest <output file>");
            System.exit(-1);
        }
        try 
        {
            raf = new RandomAccessFile(args[0], "rw");
            char a = 'a';
            byte b = 2;
            String c = "abc";
            byte[] b2 = {'a', 'b', 'c'};
            raf.write(b);	//02
            raf.write(b2, 0, b2.length);//61 62 63
            raf.writeBoolean(true);//01
            raf.writeChar(a);//00 61
            raf.writeBytes(c);//61 62 63
            raf.writeChars(c);//00 61 00 62 00 63
            raf.writeUTF(c);//00 03 61 62 63
            raf.writeUTF("abc\n");//00 04 61 62 63 0a
            System.out.println("Length of file: " + raf.length());
        } 
        catch (IOException e) 
        {
            System.err.println(e);
        } 
        finally 
        {
            try 
            {
                raf.close();
            }
            catch (Exception e) 
            { 
                System.err.println(e);
            }
        }
    }
}

运行程序:
java RandomTest output
结果:
Length of file: 27
 
output文件包含的内容用十六进制表示为:
02 61 62 63 01 00 61 61 62 63 00 61 00 62 00 63 00 03 61 62 63 00 04 61 62 63 0a

————————————————————————————

例2:下面的程序把一个文件复制成另一个文件。

//Program Name: RandCopy.java
import java.io.*;
class RandCopy 
{
    public static void main(String args[]) 
    {
        RandomAccessFile raf1 = null, raf2 = null;
        long fileSize = -1;
        byte[] buffer;
        if(args.length != 2) 
        {
            System.out.println("Usage: java RandCopy <file1><file2>");
            System.exit(0);
        }
        if(args[0].equals(args[1])) 
        {
            System.out.println(args[0]);
            System.out.println("File cannot copied onto itself!");
            System.exit(-1);
        }
        try 
        {
            raf1 = new RandomAccessFile(new File(args[0]), "r");
            //其中raf1表示原文件,以只读方式打开, 
            fileSize = raf1.length();//获得原文件的长度
        } 
        catch (IOException e) 
        {
            System.out.println("Cannot find " + args[0]);
            System.exit(-1);
        }
        try 
        {
            raf2 = new RandomAccessFile(new File(args[1]), "rw");
            // raf2表示新复制文件,以读写方式打开
        } 
        catch (IOException e) 
        {
            System.out.println("Cannot open " + args[1]);
            System.exit(-1);
        }
        buffer = new byte[(int)fileSize];
        try 
        {
            raf1.readFully(buffer, 0, (int) fileSize);
            //一次把所有数据读入内存
            raf2.write(buffer, 0, (int) fileSize);
            //全部写出到新文件中
        } 
        catch(IOException e) 
        {
            System.out.println("Cannot copy file " + args[0] + "to " + args[1]);
        } 
        finally 
        {
            try 
           {
               raf1.close();

⌨️ 快捷键说明

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