用java编写程序 1.建立一个文本文件,输入英语短文.编写一个程序,统计该文件中英文字母的个数, java创建一个文本文件

\u7528java\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5206\u522b\u7edf\u8ba1\u5e76\u8f93\u51fa\u6587\u672c\u6587\u4ef6\u4e2d\u5143\u97f3\u5b57\u6bcda,e,i,o,u\u7684\u4e2a\u6570

public static void main(String[] args) throws Exception {
File f = new File("C:\\Users\\\u53f2\u6587\u5929\\Desktop\\\u65b0\u5efa\u6587\u672c\u6587\u68631.txt");
InputStream in = new FileInputStream(f);
InputStreamReader reader = new InputStreamReader(in);
// \u8fd9\u53e5\u8bdd\u4e0d\u8981\u5ffd\u7565\uff0c\u5b83\u5bf9\u4f60\u4ee5\u540e\u7f16\u5199\u9ad8\u6027\u80fdIO\u6d41\u6709\u81f3\u5173\u91cd\u8981\u7684\u7406\u89e3\uff0c\u6211\u5efa\u8bae\u4f60\u5148\u4e86\u89e3\u4e00\u4e0b\u7f13\u51b2\u533a\u7684\u6982\u5ff5
BufferedReader buffered = new BufferedReader(reader);
String text = "";
for (String line = buffered.readLine(); null != line; line = buffered.readLine()) {
text += line;
}
buffered.close();
int aTotal = 0;
int eTotal = 0;
int iTotal = 0;
int oTotal = 0;
int uTotal = 0;
for (int index = 0; index < text.length(); index++) {
if ('a' == text.charAt(index))
aTotal++;
if ('e' == text.charAt(index))
eTotal++;
if ('i' == text.charAt(index))
iTotal++;
if ('o' == text.charAt(index))
oTotal++;
if ('u' == text.charAt(index))
uTotal++;

}
System.out.println("a\u7684\u4e2a\u6570\u662f\uff1a" + aTotal);
System.out.println("e\u7684\u4e2a\u6570\u662f\uff1a" + eTotal);
System.out.println("i\u7684\u4e2a\u6570\u662f\uff1a" + iTotal);
System.out.println("o\u7684\u4e2a\u6570\u662f\uff1a" + oTotal);
System.out.println("u\u7684\u4e2a\u6570\u662f\uff1a" + uTotal);

}

\u53ef\u4ee5\u901a\u8fc7\u201cFileOutputStream\u201d\u521b\u5efa\u6587\u4ef6\u6587\u672c\u6587\u4ef6\uff0c\u4e4b\u540e\u8fc7\u201cOutputStreamWriter\u201d\u6d41\u7684\u5f62\u5f0f\u8fdb\u884c\u6587\u4ef6\u5185\u5bb9\u5b58\u50a8\uff0c\u4e3e\u4f8b\uff1a
OutputStreamWriter pw = null;//\u5b9a\u4e49\u4e00\u4e2a\u6d41
pw = new OutputStreamWriter(new FileOutputStream(\u201cD:/test.txt\u201d),"GBK");//\u786e\u8ba4\u6d41\u7684\u8f93\u51fa\u6587\u4ef6\u548c\u7f16\u7801\u683c\u5f0f\uff0c\u6b64\u8fc7\u7a0b\u521b\u5efa\u4e86\u201ctest.txt\u201d\u5b9e\u4f8b
pw.write("\u6211\u662f\u8981\u5199\u5165\u5230\u8bb0\u4e8b\u672c\u6587\u4ef6\u7684\u5185\u5bb9");//\u5c06\u8981\u5199\u5165\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u591a\u6b21write
pw.close();//\u5173\u95ed\u6d41
\u5907\u6ce8\uff1a\u6587\u4ef6\u6d41\u7528\u5b8c\u4e4b\u540e\u5fc5\u987b\u53ca\u65f6\u901a\u8fc7close\u65b9\u6cd5\u5173\u95ed\uff0c\u5426\u5219\u4f1a\u4e00\u76f4\u5904\u4e8e\u6253\u5f00\u72b6\u6001\uff0c\u76f4\u81f3\u7a0b\u5e8f\u505c\u6b62\uff0c\u589e\u52a0\u7cfb\u7edf\u8d1f\u62c5\u3002


public class Read {
    public static void main(String[] args) throws Exception {
        long size = readFileByChars("D://test.txt");
        write("D://test1.txt",size);

    }
    public static long readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        int num =0;
        try {
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 对于windows下,rn这两个字符在一起时,表示一个换行。
                // 但如果这两个字符分开显示时,会换两次行。
                // 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。
                if (((char) tempchar) != 'r') {
                    System.out.print((char) tempchar);
                }
                if (tempchar>= 'A' && tempchar<= 'Z' ||tempchar>= 'a' && tempchar<= 'z'){
                    num++;
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return num;
    }
    public static void write(String fileName,long size) throws IOException {
        File file = new File(fileName);
        Writer writer = null;
        try {
            writer =new FileWriter(new File(fileName));
            writer.write("英文字母共有:"+size);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (writer!= null){
                writer.close();
            }
        }
    }

}

 

第二题:
public class Read {
    public static void main(String[] args) throws Exception {
       readFile("D://test.txt");
    }
    public static void readFile(String fileName ) throws Exception {
        BufferedReader isr = new BufferedReader(new FileReader(fileName));
        String str = null;
        int allnumber =1;
        str = isr.readLine();
        String [] strings = str.split("\\s+");
        float [] sum ={Float.valueOf(strings[0]),Float.valueOf(strings[1]),Float.valueOf(strings[2])};
        float [] min ={Float.valueOf(strings[0]),Float.valueOf(strings[1]),Float.valueOf(strings[2])};
        float [] max ={Float.valueOf(strings[0]),Float.valueOf(strings[1]),Float.valueOf(strings[2])};
        System.out.println(str);
        while ((str = isr.readLine())!= null){
            System.out.println(str);
            deal(str,sum,min,max);
            allnumber++;
        }
        if (isr!= null){
            isr.close();
        }
        System.out.println("三门课最大值:"+max[0]+""+max[1]+""+max[2]);
        System.out.println("三门课最小值:"+min[0]+""+min[1]+""+min[2]);
        System.out.println("三门课平均值:"+sum[0]/allnumber+""+sum[1]/allnumber+""+sum[2]/allnumber);
    }
    public static void deal(String str, float[] sum, float[] min, float[] max){
        String [] strings = str.split("\\s+");
        for (int i=0;i<3;i++) {
            sum[i] = sum[i] + Float.valueOf(strings[i]);
            if (max[i] < Float.valueOf(strings[i])) {
                max[i] = Float.valueOf(strings[i]);
            }
            if (min[i] > Float.valueOf(strings[i])) {
                min[i] = Float.valueOf(strings[i]);
            }
        }
    }
}

 




  package mainpackage;
  /**
  * 首先  得知道文件位置
  * 然后 绑定文件的字符流
  * 接着 得到字符流之后就可以每行进行字符读取
  * 最后 得到每行字符后就可以判断了
  * 注:我也是闲着没事,顺便写的。其实倒还写的有些复杂了
  * 你有什么不懂得可以随时问我
  * 在不在就不敢保证了
  *
  */
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;

  public class ReadFileBytes {
  public static void main(String[] args) throws IOException {
  String fileDir = "H:/read.txt";                //文件路劲
  File file = new File(fileDir);                 //实例化文件
  FileReader in =  new FileReader (file);        //实例化文件读取对象
  BufferedReader bf = new BufferedReader(in);    //绑定字符流
  String str ;                                   //拼接字符对象
  String deStr = "";                            //拼接结果
  int len = 0;                                     //字母个数
  while ((str = bf.readLine() )!=null) {               //循环行,获取每行的字符并且进行拼接
  deStr+=str;
  }
  byte[] b = deStr.getBytes();                      //将拼接的字符串beye化
  for(byte s:b){                                    //循环数据 如果为字母 就将长度加一
  if(s>='A'&&s<='Z'||s>='a'&&s<='z'){
  len++;
  }
  }
  System.out.println(len);                        //打印长度
  }
  }


tretches before us in the constant panorama

扩展阅读:java编程零基础入门 ... java编程常用软件 ... 为什么都不建议java转测试 ... java简单入门程序代码 ... java代码生成器 ... java编程代码大全免费 ... 初学编程必背50个 ... java编程怎么建立类 包 ... 编写一个简单的java程序 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网