java 输入文字 个数9_从键盘输入一串字符串,编写一个java程序实现统计,输出有几个大写字母,几个小写字母,和几个数字,并把大...

这是一个Java程序,用于读取指定文件并统计其中的大写字母、小写字母和数字个数。程序包含FileUtil类用于处理文件读写,MainView类用于创建用户界面,Launcher类包含主方法。用户通过界面输入文件路径,程序将结果显示在文本区域。
摘要由CSDN通过智能技术生成

这个是处理文件的类FileUtil:

=================================================

package org.xhome.leon.test;

import java.io.*;

public class FileUtil {

FileReader fr;

BufferedReader br;

FileWriter fw;

BufferedWriter bw;

String source = "";

public int upCaseNum = 0;

public int lowerCaseNum = 0;

public int numerNum = 0;

public int otherNum = 0;

public void initReaders(String fileName) throws FileNotFoundException {

fr = new FileReader(fileName);

br = new BufferedReader(fr);

}

public void initWriters(String fileName) throws IOException {

fw = new FileWriter(fileName);

bw = new BufferedWriter(fw);

}

public void readFile(String fileName) throws FileNotFoundException {

this.initReaders(fileName);

this.source = "";

upCaseNum = 0;

lowerCaseNum = 0;

numerNum = 0;

otherNum = 0;

String sr;

try {

while ((sr = br.readLine()) != null) {

source = source.concat(sr);

}

if (source != null) {

char[] myChars = source.trim().toCharArray();

for (int i = 0; i < myChars.length; i++) {

if (myChars[i] >= 'a' && myChars[i] <= 'z') {

lowerCaseNum++;

} else if (myChars[i] >= 'A' && myChars[i] <= 'Z') {

upCaseNum++;

} else if (myChars[i] >= '0' && myChars[i] <= '9') {

numerNum++;

} else {

otherNum++;

}

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

this.closeReaderIO();

}

}

public void writeFile(String infileName,String outfileName, boolean isToUperCase) throws IOException {

this.initWriters(outfileName);

this.initReaders(infileName);

String st;

try {

if (isToUperCase) {

while ((st = br.readLine()) != null) {

st = st.toUpperCase();

try {

bw.write(st);

bw.newLine();

bw.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} else {

while ((st = br.readLine()) != null) {

st = st.toLowerCase();

try {

bw.write(st);

bw.newLine();

bw.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

this.closeReaderIO();

this.closeWriterIO();

}

}

public void closeReaderIO() {

if (this.fr != null) {

try {

this.fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (this.br != null) {

try {

this.br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public void closeWriterIO() {

if (this.fw != null) {

try {

this.fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (this.bw != null) {

try {

this.bw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

下面是界面编写的类:

=========================================================

package org.xhome.leon.view;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.FileNotFoundException;

import java.io.IOException;

import javax.swing.*;

import org.xhome.leon.test.FileUtil;

public class MainView extends JFrame{

JPanel upPanel = new JPanel();

JPanel downPanel = new JPanel();

JLabel rlabel = new JLabel("读入文件绝对路径");

JLabel wlabel = new JLabel("写出文件绝对路径");

JTextField rjtf = new JTextField();

JTextField wjtf = new JTextField();

JButton rbtn = new JButton("读取");

JButton wbtn = new JButton("写入");

JButton upORlow = new JButton("小写方式写入");

JSplitPane jsp;

JTextArea jta = new JTextArea();

boolean isUperCase = false;

FileUtil ft;

public MainView(){

ft = new FileUtil();

this.setBounds(200, 100, 600, 400);

this.init();

8c1eea3536e4e7e7baf6ac7f4a808df0.png

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JOptionPane.showMessageDialog(this, "请注意,文件路径分隔符为 / ,而非 \\,比如D盘下的test.txt\n为D:/test.txt");

}

public void init(){

upPanel.setLayout(null);

rlabel.setBounds(5, 5, 150, 30);

wlabel.setBounds(5, 80, 150, 30);

rjtf.setBounds(165, 5, 215, 30);

wjtf.setBounds(165, 80, 215, 30);

rbtn.setBounds(505, 5, 60, 30);

wbtn.setBounds(505, 80, 60, 30);

upORlow.setBounds(5, 120, 120, 30);

rbtn.addActionListener(new MyButtonListener());

wbtn.addActionListener(new MyButtonListener());

upORlow.addActionListener(new MyButtonListener());

upPanel.add(rlabel);

upPanel.add(wlabel);

upPanel.add(rjtf);

upPanel.add(wjtf);

upPanel.add(rbtn);

upPanel.add(wbtn);

upPanel.add(upORlow);

jta.setEditable(false);

downPanel.add(jta);

jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upPanel, downPanel);

jsp.setDividerLocation(170);

jsp.setEnabled(false);

this.add(jsp);

}

class MyButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(rbtn)){

try {

ft.readFile(rjtf.getText().toString().trim());

jta.setText("大写字母:"+ft.upCaseNum+"\n\n");

jta.append("小写字母:"+ft.lowerCaseNum+"\n\n");

jta.append("数字:"+ft.numerNum+"\n\n");

jta.append("其他:"+ft.otherNum+"\n\n");

} catch (FileNotFoundException e1) {

JOptionPane.showMessageDialog(MainView.this, "读取错误,请确认文件路径是否正确");

return;

}

7762691.html

}

else if(e.getSource().equals(wbtn)){

String inFile = rjtf.getText().toString().trim();

if(inFile != null && !inFile.equals("")){

try {

ft.writeFile(inFile, wjtf.getText().toString().trim(), isUperCase);

JOptionPane.showMessageDialog(MainView.this, "写入成功!");

} catch (IOException e1) {

JOptionPane.showMessageDialog(MainView.this, "写入错误,请确认文件路径是否正确");

return;

}

}

}

else if(e.getSource().equals(upORlow)){

if(isUperCase){

isUperCase = false;

upORlow.setText("小写方式写入");

}

else{

isUperCase = true;

upORlow.setText("大写方式写入");

}

}

}

}

}

===============================================

下面是一个装main方法的类:

package org.xhome.leon.main;

import org.xhome.leon.view.MainView;

public class Launcher {

public static void main(String[] args) {

new MainView();

}

}

======================================

下面是我用自己写的那个java文件测试的结果截图:

=======================================

ps:这个是看了楼主的要求后写的。用的是窗口程序实现,如果不用界面,你可以直接用FileUtil这个类进行操作就可以了。 希望对你用帮助 ^ ^

7762691.html

取消

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值