java文件

 

一、Java Web相关

 

- Servlet与Spring

- 解释:Servlet是Java生态中用于处理HTTP请求的技术,早期用于开发网站后端。但Spring出现后,因其更先进、功能更强大且内置了Tomcat(Java生态中的HTTP服务器,提供Servlet框架 ),在企业开发中逐渐取代了Servlet。现在企业开发几乎不再使用Servlet,而是使用Spring。

- 代码示例:无直接代码示例,但理解Spring和Servlet关系对后续代码编写有架构层面的指导。

 

二、Java文件操作

 

- 文件系统操作(File类)

- 解释: File 类用于对文件系统进行操作,如创建、删除、重命名文件或目录等。通过 File 类的方法可以获取文件或目录的相关信息,如 list() 方法返回目录下所有文件名, listFiles() 返回目录下所有文件的 File 对象。还可进行创建目录( mkdir() 、 mkdirs()  )、重命名或移动文件( renameTo(File dest)  )等操作。

- 代码示例:

 

import java.io.File;

public class FileExample {

    public static void main(String[] args) {

        File directory = new File("C:/Users/1/Desktop/myData/project/classroom_code/java15&16/javaee/project");

        String[] fileNames = directory.list();

        for (String name : fileNames) {

            System.out.println(name);

        }

        File[] files = directory.listFiles();

        for (File file : files) {

            System.out.println(file.toString());

        }

    }

}

 

 

- 文件内容操作(字节流和字符流)

- 字节流(InputStream和OutputStream)

- 解释:字节流用于以字节为单位读写文件,适用于处理所有类型的文件。 InputStream 用于从文件读取数据到内存, OutputStream 用于将内存中的数据写入文件。常见子类如 FileInputStream 和 FileOutputStream  。读取时一般先创建字节数组,通过 read(byte[] b) 方法读取数据到数组中,写入时通过 write(byte[] b) 等方法将字节数组内容写入文件。

- 代码示例:

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class ByteStreamExample {

    public static void main(String[] args) {

        try (FileInputStream inputStream = new FileInputStream("test.txt");

             FileOutputStream outputStream = new FileOutputStream("output.txt")) {

            byte[] buffer = new byte[1024];

            int length;

            while ((length = inputStream.read(buffer)) != -1) {

                outputStream.write(buffer, 0, length);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

- 字符流(Reader和Writer)

- 解释:字符流用于以字符为单位读写文件,更适合处理文本文件。 Reader 用于读取字符数据, Writer 用于写入字符数据。常见子类如 FileReader 和 FileWriter  。读取方法如 read(char[] cbuf)  ,写入方法如 write(String str) 等。

- 代码示例:

 

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class CharStreamExample {

    public static void main(String[] args) {

        try (FileReader reader = new FileReader("input.txt");

             FileWriter writer = new FileWriter("output.txt")) {

            char[] buffer = new char[1024];

            int length;

            while ((length = reader.read(buffer)) != -1) {

                writer.write(buffer, 0, length);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

- 资源管理(try - with - resources)

- 解释:传统文件操作中,如果在读取或写入过程中抛出异常,可能导致 close() 方法无法执行,从而造成资源泄漏。 try - with - resources 语法能在 try 块执行结束后自动调用资源(如 InputStream 、 OutputStream 等)的 close() 方法,确保资源正确释放。

- 代码示例:

 

import java.io.FileInputStream;

import java.io.IOException;

public class TryWithResourcesExample {

    public static void main(String[] args) {

        try (FileInputStream inputStream = new FileInputStream("test.txt")) {

            byte[] buffer = new byte[1024];

            int length;

            while ((length = inputStream.read(buffer)) != -1) {

                // 处理读取到的数据

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

三、Java基础语法及其他

 

- 变量交换

- 解释:在Java中交换两个变量的值,通常借助一个临时变量。先将一个变量的值赋给临时变量,再将另一个变量的值赋给第一个变量,最后将临时变量的值赋给第二个变量。

- 代码示例:

 

public class VariableSwap {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

        int tmp = x;

        x = y;

        y = tmp;

        System.out.println("x = " + x + ", y = " + y);

    }

}

 

 

- 方法返回值

- 解释:Java中一个方法只能返回一个值。若要返回多个值,可将多个值封装到一个类中(创建一个包含多个成员变量的类,将需要返回的值作为成员变量 ),或者使用输出型参数(如方法参数为数组,在方法内部修改数组内容 )。

- 代码示例(封装类):

 

class MultipleValues {

    private int value1;

    private int value2;

    public MultipleValues(int value1, int value2) {

        this.value1 = value1;

        this.value2 = value2;

    }

    public int getValue1() {

        return value1;

    }

    public int getValue2() {

        return value2;

    }

}

public class MethodReturnValue {

    public static MultipleValues calculate() {

        int a = 5;

        int b = 10;

        return new MultipleValues(a, b);

    }

    public static void main(String[] args) {

        MultipleValues result = calculate();

        System.out.println("Value 1: " + result.getValue1() + ", Value 2: " + result.getValue2());

    }

}

 

 

以上代码和解释涵盖了图片中涉及的主要Java知识点,通过代码示例能更直观地理解相关概念和操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值