Rhine_danji_ys 2022-08-15 20:39 采纳率: 100%
浏览 105
已结题

读取文件后,用substring(i,j)截取字符串出现不正常情况

文件里只有一句话,前面是文件取出的字符串,后面打印的是substring(8,i)截取后的字符串,为什么会出现两种情况?如下所示,

img

文件中的文字是"keyword="的格式写入的,但是截取字符串后却会多一个字符"=",百思不得其解。
主要代码是这样:👌🏻

List<Account> accounts = new ArrayList<>();
        String words = "";
//      String words = "keyword=wchat.com;微信;user=name44;password=845965*Zoo\r\n" + 
//                "keyword=wchat.com;微信;user=name44;password=77854530*Traggle";//若是编译期直接赋值无此种情况
        words = FileEncode.readEncodeFile1("src/file/strategy1.txt");//用文件读取后words正常但是截取字符串结果错误
        String[] lineWords = words.split("\n");
        System.out.println("words:" + words);
        for (int i = 0; i < lineWords.length; i++) {
            int keyIndex = lineWords[i].lastIndexOf(";user=");
            int userIndex = lineWords[i].lastIndexOf(";password=");
            if (keyIndex != -1 && userIndex != -1) {
                String keyword = lineWords[i].substring(8, keyIndex);//i=0时,截取后多了个=
                String user = lineWords[i].substring(keyIndex + 6, userIndex);
                String password = lineWords[i].substring(userIndex + 10);
                Account account = new Account();
                account.setKeyword(keyword);
                account.setUser(user);
                account.setPassword(password);
                accounts.add(account);
            } else {
                System.out.println("找不到user和password");
            }
        }
        for (Account a : accounts) {
            System.out.println(a.toString());
        }
        

估计是读文件导致出现问题

    /**
     * 读取文件 获取account列表  读取整个文件
     * @param relative
     * @param key 暂未使用
     * @return
     */
    public static String readEncodeFile1(String relative) {
        File file = new File(relative);
        FileInputStream fis = null;
        String words = "";
        try {
            fis = new FileInputStream(file);
            byte[] buf = new byte[1024]; //数据中转站 临时缓冲区  字节读取不会出现乱码
            int length = 0;
            while ((length = fis.read(buf)) != -1) {
                words += new String(buf, 0, length);//将file中的内容遍历出来
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到指定路径的文件:" + "" + relative);//项目的根路径不知道
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("fis.read()异常");
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return words;
    }

Account实体类:

public class Account {
    
    private String keyword;//关键字
    private String user;//账户名
    private String password;//密码
    
    public String getKeyword() {
        return keyword;
    }
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public int hashCode() {
        return super.hashCode();
    }
    @Override
    public String toString() {
        return "keyword=" + this.keyword +";user=" + this.user + ";password=" + this.password;
    }
    
}

  • 写回答

4条回答 默认 最新

  • 爱吃苦瓜的猿 2022-08-15 22:09
    关注

    你的代码没有问题,根据现象分析只有第一行有这个问题,应该是windows下txt文件编码格式的问题。
    可能的原因:使用了带BOM的UTF8格式。
    UTF-8和带BOM的UTF-8的区别就是有没有BOM。即文件开头有没有 U+FEFF。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月25日
  • 已采纳回答 8月17日
  • 修改了问题 8月16日
  • 修改了问题 8月16日
  • 展开全部

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改