首页 > 其他分享 >三月六号

三月六号

时间:2024-03-06 19:56:43浏览次数:19  
标签:文件 String System 三月 六号 println new out

今日学习

所花时间  1小时
代码量  162
搏客量 一篇
了解到的知识点 英语单词链

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class english {
    public static void main(String []srgs){

        File file = new File("C:\\Users\\DELL\\Desktop\\资料\\JAVA TWO\\input.txt");
        System.out.println("判断文件格式是否正确:");
        if(file.exists() && file.length() == 0) {
            System.out.println("  文件为空!");
            System.exit(0);
        } else if(oneWord()){
            System.out.println("  文件只有一個單詞!");
            System.exit(0);
        }else{
            System.out.println("  文件内容有效。");
            achieve();
        }
    }
    public static boolean oneWord(){
        ArrayList<String> list = new ArrayList<String>();
        String pathname = "C:\\Users\\DELL\\Desktop\\资料\\JAVA TWO\\input.txt";
        int i = 0;
        try (
                FileReader reader = new FileReader(pathname);
                BufferedReader br = new BufferedReader(reader)
                // 建立一个对象,它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            //网友推荐更加简洁的写法
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                list.add(line);
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(i<=1){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 获取单词首尾字母的方法
     */
    public static void print(){
        String a = "sadfg";
        System.out.println(a.substring(0,1));//获取首字母
        System.out.println(a.substring(a.length()-1,a.length()));//获取尾字母
    }


    /*
     * 读取txt文件,并且存到ArrayList集合中,找出接龙单词 存入ArrayList1集合,
     * 把ArrayList1集合写入output1文件
     */
    public static void achieve() {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> list1 = new ArrayList<String>();//存储遍历结果

        String pathname = "C:\\Users\\DELL\\Desktop\\资料\\JAVA TWO\\input.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件
        String writename = "C:\\Users\\DELL\\Desktop\\资料\\JAVA TWO\\output.txt";
        //防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw;
        //不关闭文件会导致资源的泄露,读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
             FileWriter writer = new FileWriter(writename);
             BufferedWriter out = new BufferedWriter(writer)
        ) {
            String line;
            //网友推荐更加简洁的写法
//            System.out.println("遍历readline");
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
//                System.out.println(line);
                list.add(line);
            }
            System.out.println("遍历所有单词集合(arraylist):");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }


            String word0 = list.get(0);
            list1.add(word0);
            String word = " ";//用来存储单词
            for(int j = 1;j < list.size();j++){
                word = list.get(j);
                if(word.substring(0,1).equals(word0.substring(word0.length()-1,word0.length())) )
                //不能用“==”判断字符串的等价,要用equals!!!!!!!!!!!!!!!!!!!!!!!!
                {
                    list1.add(word);
                    word0 = word;
                }
            }
            //打印list1
            System.out.println("遍历list1");
            //把list1中的结果写入output1.txt文件
            for (int g = 0; g < list1.size(); g++){
                System.out.println(list1.get(g));
                out.write(list1.get(g));
                out.write(" ");
            }

            System.out.println("程序结束!");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * txt文件的读取
     */
    public static void readFile() {
        String pathname = "input.txt";
        //不关闭文件会导致资源的泄露,读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     *txt文件的写入
     */
    public static void writeFile() {
        try {
            File writeName = new File("C:\\Users\\DELL\\Desktop\\资料\\JAVA TWO\\output.txt");
            writeName.createNewFile(); //创建新文件,有同名的文件的话直接覆盖
            try (
                    FileWriter writer = new FileWriter(writeName);
                    BufferedWriter out = new BufferedWriter(writer)
            ) {
                out.write("我会写入文件啦!\r\n"); // \r\n为换行
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

标签:文件,String,System,三月,六号,println,new,out
From: https://www.cnblogs.com/youxiandechilun/p/18057410

相关文章

  • 三月五日 课堂练习
    课堂练习01题目:计算最长英语单词链。一、题目内容:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。二......
  • 九下三月上旬日记
    3.1闲话详见NOI2024联合省选暨HEOI2024游记。做题纪要luoguB3908异或构造题?构造\(x=\bigoplus\limits_{i=1}^{n}a_{i}\)即可。点击查看代码intmain(){ lln,a,x=0,i; cin>>n; for(i=1;i<=n;i++) { cin>>a; x^=a; } cout<<x<<""&......
  • 三月三日
    今天主要是关于c3p0的配置,我用的是eclipse2020版的,主要遇到的问题是在src目录下创建c3p0-config.xml时找不到file。 看到图理最上方大写的File就可以了,还有图中的代码是测试c3p0的。还有我自己在网上找到的xml的具体内容,<?xmlversion="1.0"encoding="UTF-8"?><c3p0-con......
  • 三月三
    阳历的三月三好像不是什么特殊的日子,不过还是感觉这个日期很特别,正好,自己好久不写东西了。这个寒假自己经历了太多太多了,补课,作业,串亲戚,饭局,还有主角——抑郁症。没错,过了个年,我也彻底崩溃了。低沉的情绪先是如潮水般涌来,然后又凝固变重,将我埋没,让我窒息。在这摊泥沼中,外面的中......
  • 一月:January 二月:February 三月:March 四月:April 五月:May 六月:June 七月:July 八月:Augus
    为什么“星期一”是Monday而不是Sunday?一周七天到底是怎么命名出来的?在英语中, 一周七天是以神和行星命名的: Sun(太阳)Moon(月亮)Mars(火星)Mercury(水星) Jupiter(木星)Venus(金星)Saturn(土星) 现在的7天的不同的拼法, 是历史演进的结果。  #01   Monday   ......
  • 【彩虹六号】各个地图有意思的玩法笔记(自用)
    【彩虹六号】各个地图有意思的玩法笔记(自用)每次看到一些有趣的方法总是忘记,干脆做个笔记吧运河运河二楼外墙摔炮听切运河二楼外墙摔炮听切实战方向放大需要注意这个地方可以收人头/被抓站在钢琴房偷天窗天窗偷人法先给天窗标点去钢琴房敲洞仅限开局三十秒木屋A......
  • 阿里p7闭关三月:整理出了这份java秋招面试必备指南
    从去年互联网寒冬的裁员潮,到今年受疫情影响的春招消失,金三银四变成铜三铁四,不过有一些朋友还是拿到了自己心怡的offer,有一些朋友还在为面试发愁,今天给大家分享一个阿里大佬闭关三个月的整理出的java秋招面试必备指南。大厂的面试从来不会是固定的,特别是像阿里这种一线互联网公司,想......
  • 七月六号Java学习
    今天学习了键盘输入,下载idea,了解到怎么创建项目,模块,并写一个helloworld的代码  ......
  • 2021-DASCTF-三月赛-Writeup
    文章目录WEBBestDBez_serializebaby_flaskez_loginMISC签到简单的png隐写雾都孤儿小田的秘密Ascii_art问卷调查和团队的师傅们组队拿了个第十,师傅们带飞,我就是团队的MVP(MostVegetablePeople)WEBBestDB简单的SQL注入/?query=mochu"or/**/1=1%23/?query=mochu"order/**/by/**/......
  • CF三月D题题解
    cf1798d题意:重排序列,使得其中连续子序列和的绝对值最大的最大值小于序列最大值减最小值,序列和为0考虑这样一种构造方案:正负数分类,0直接不管然后记录当前和sum,当sum非负时,加上一个负数,当sum是负数时,加上一个正数即可正确性证明:显然前缀和都是合法的。考虑计算前缀和数组,满足......