一、File类二、字节流和字符流

本文介绍了Java中的File类,用于文件和目录的操作,包括创建、删除以及获取文件属性的方法。同时,详细讲解了字节流和字符流的基本使用,如FileOutputStream和FileInputStream的write方法,以及FileReader和FileWriter的read方法。示例代码展示了如何读写文件以及字符串与字节数组的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、File类
File类是通过路径名定位文件和文件夹的,new File的时候,传入(String)、(String、String)、(File、String),能实现创建File对象。可以传入相对路径或绝对路径。再用对象createNewFile、mkdir、mkdirs来创建文件或文件夹;而删除操作则是通过delete方法来操作,删除的如果是文件则直接,如果是文件夹则需要空。

以上都是返回boolean来告知是否操作成功。

File类提供了一系列获取的方法:

getAbsolutePath();获取绝对路径、getName();获取文件或文件夹的名字、getParent();获取父路径、length();获取文件(文件夹无法获取准确的)大小、listFile();获取文件夹(不能文件)下的所有内容,可以搭配增强for遍历。

File类提供了一系列判断的方法:

isDirectory();判断是否为文件夹、isFile();判断是否文件、isExist();判读是否存在。都是返回boolean。


        File file = new File("D:\\MyFileTest\\aaa");

        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                if (file.getName().endsWith(".java")) {System.out.println(file);}
            } else {
                listJava(file);
            }
        }

二、字节流和字符流
所有的流对象,在结束使用时都需要调用.close()方法,结束本线程对该文件的使用,否则占用着该文件其他线程无法使用之。
流对象分为字节流和字符流,字节流分为InputStream字节输入流、OutputStream字节输出流;字符流分为Reader字符输入流、Writer字符输出流,以上都是接口。

OutputStream字节输出流有接口:FileOutputStream,常用方法有write(int/byte[]);byte[]可以写成(byte[],int 开始的索引,int 写出的长度)将内容写入对应file文件中:
FileOutputStream fos = new FileOutputStream("study_day11\\abc\\1.txt");
    fos.write(97);
    fos.write(98);
    byte[] bs = new byte[] {65, 66, 67, 68, 69};
    fos.write(bs);
    fos.write(bs, 2, 3);
    fos.close();

write()的形参有:int、byte[]


InputStream字节输入流有接口:FileInputStream,常用方法有read(int/byte[]);括号中设置读取的字节数量,由于读到的是int,所以需要强转成char:
FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    int b = fis.read();

System.out.println((char) b);

//如果要循环读则:

int b;

while ((b = fis.read()) != -1) {System.out.println((char) b);}
    fis.close();

字节输入流循环byte数组读本地写到程序:
FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    // 定义数组保存读取的内容 数组的长度实际应该写1024的整数倍,建议写1024 * 8
    byte[] buf = new byte[3];
    int len;

while ((len = fis.read(buf)) != -1) {System.out.println(new String(buf, 0, len));

}fis.close();

String和byte[]互转:
byte[]转String:new String(byte bytes[], int offset, int length)
String转成byte[]:String对象.getBytes()

Reader字符输入流有接口:FileReader,常用方法有read(int/byte[]);括号中设置读取的字节数量,由于读到的是int,所以需要强转成char:

FileReader fr = new FileReader("study_day11\\abc\\4.txt");
    int ch;
    while ((ch = fr.read()) != -1) {System.out.println((char) ch);}
    fr.close();
字符输入流循环char数组读写:
FileReader fr = new FileReader("study_day11\\abc\\4.txt");
    char[] chs = new char[3];
    int len;
    while ((len = fr.read(chs)) != -1) {System.out.println(new String(chs, 0 , len));}
    fr.close();

Writer字节输出流有接口:FileWriter,常用方法有write(int/char[]/String);String和char[]可以写成(String a/char[] a,int 开始的索引,int 写出的长度)将内容写入对应file文件中:

FileWriter fw = new FileWriter("study_day11\\abc\\3.txt", true);
    fw.write(97);
    fw.write(“123”);
    fw.close();

write()的形参有:int、char[]、String

### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值