码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 输入输出程序开发

时间:2019-01-03 21:33:39      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:字符流   list()   put   调用   创建   begin   info   html   生成   

参考:

java中 静态方法和非静态方法的区别

字符流的输入和输出

java文件创建、删除、读取、写入操作大全

Java键盘输入并且写入文件

File类的isDiretory

Java统计子串在字符串中出现的次数

java File类list()和listFile()的方法区别

统计某个路径下所有的java文件,以及统计代码数量

java 去除首尾空格trim()方法

 

 

 技术分享图片

import java.io.*;




public class HandInput{
    //生成文件路径
    //private static String path  = "Documents/code/java 输入输出程序开发";

    //文件路径+名称
    private static String filenameTemp;

    /**
     * 创建文件
     * @param fileName 文件名称
     * @return 是否创建成功,成功则返回true
     */
    public static boolean createFile(String fileName){
        Boolean bool = false;
        filenameTemp = fileName+".txt"; //文件路径+名称+文件类型 ?
        File file = new File(filenameTemp);
        try{
            //如果文件不存在,则创建新的文件
            if(!file.exists()){
                file.createNewFile();
                bool = true;
                System.out.println("success create file, the file is "+filenameTemp);
                //创建InputStreamReader对象,用来读取字符流
                //缓冲指定 字符流的输入
                //FileInputStream->InputStreamReader->BufferedReader
                BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
                BufferedWriter buf2 = new BufferedWriter(new FileWriter(filenameTemp));
                String str = buf.readLine();
                while(!str.equals("end#")){
                    buf2.write(str);
                    buf2.newLine();
                    str = buf.readLine();
                }
                buf2.flush(); //刷新该流的缓冲,即将该流输出到目的
                buf2.close();
                buf.close();
            }
            else {
                System.out.println("file is exist");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

        return bool;
    }

    public static void main(String[] args){
        createFile("f");
    }
}

 

技术分享图片

 

import java.io.*;

public class WordCount {
    public static void main(String[] args) throws IOException {
        File file = new File("article.txt");
        //isDirectory()是检查一个对象是否是文件夹
        if(!file.exists() || file.isDirectory()){
            throw new FileNotFoundException();
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        int count=0;

        String tmp = null;
        tmp = br.readLine();

        while(tmp!=null){
            tmp = tmp.toLowerCase();
            System.out.println(tmp);
            while(tmp.indexOf("hello")!=-1){ //调用String类的indexOf(String str)方法,返回第一个相同字符串出现的下标
                count++;
                tmp = tmp.substring(tmp.indexOf("hello")+5); //调用String类的substring(int beginIndex)方法,获得第一个相同字符串出现后的字符串
            }
            tmp = br.readLine();

        }
        System.out.println("单词hello在文章article中出现的次数为: "+count);
    }
}

 

技术分享图片

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class CodeCount {
    static List<File> javalsf = new ArrayList<File>();
    static List<File> lsf = new ArrayList<File>();
    static int count =0;

    public static void main(String[] args) throws Exception{
       File file = new File("/Users/fitzroy/Documents/code/汇添富移动互联股票(000697)基金收益程序/src");
       getJavaFiles(file);
       System.out.println("有"+javalsf.size()+"个.java源文件");
       getLinesOfCode(javalsf);
    }

    public static List<File> getJavaFiles(File file){
        if(!file.exists()){
            System.out.println("file not exists");
        }
        else{
            if(file.isFile()){ //是文件
                String  filename = file.getName();
                if(filename.substring(filename.lastIndexOf(".")+1).equals("java")){ //后缀为.java的文件
                    javalsf.add(file);
                }
            }
            else{ //是文件夹
                File[] filelist = file.listFiles(); //获取文件夹中的File
                for(File files : filelist){
                    getJavaFiles(files);
                }
            }
        }

        return javalsf;
    }

    public static int getLinesOfCode(List<File> javaFiles) {
        int num = 0;
        try {
            for (File file : javaFiles) {
                System.out.println("File: " + file.getName());
                InputStreamReader is = new InputStreamReader(new FileInputStream(file));
                BufferedReader br = new BufferedReader(is);
                String str = null;
                str = br.readLine();
                while (str != null) {
                    str = str.trim();
                    if (!str.startsWith("//") && !str.equals("")) {
                        num++;
                    }
                    str = br.readLine();
                }
                count += num;
                System.out.println("lines of code: " + num);
                System.out.println("-------------------");
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("total: " + count);
        return count;
    }
}

 

JAVA 输入输出程序开发

标签:字符流   list()   put   调用   创建   begin   info   html   生成   

原文地址:https://www.cnblogs.com/fitzroy343/p/10216853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!