文件读入与输出及英文大小写转换

英文大小写转换Demo.解决文件读入与输出问题乱码问题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ToLower {
    public static final String path="F:\\ytk\\UPTOLOWER\\1.txt";
    public static final String topath="F:\\ytk\\UPTOLOWER\\2.txt";
    public static void main(String[] args){
        boolean isComp=toLower(path,topath);
//        boolean isComp=toUpper(path,topath);
        System.out.println("文件转换状态--->"+isComp);
    }
    
    /*
     * tolower
     * */
    private static boolean toLower(String path,String topath) {
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            File file=new File(path);
            if(!file.exists()) {
                System.out.println("文件不存在《《《《《《"+path);
                return false;
            }
            File toFile=new File(topath);
            if(toFile.exists()) {
                toFile.delete();
            }
            toFile.createNewFile();
            bufferedReader=
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream(file),
                                    "GBK"));
            bufferedWriter=
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    new FileOutputStream(toFile,true), "GBK"));
            int count=0;
            StringBuffer bufferTemp=new StringBuffer();
            String str="";
            String temp="";
            String tempTo="";
            while((str=bufferedReader.readLine())!=null) {
                count++;
                temp=str.replaceAll("\\s*","");
                tempTo=temp.toLowerCase();
                if(!checkLength(temp,tempTo)) {
                    System.out.println("第"+count+"行出现错误《《《《《《《"+temp);
                }
                temp=count+"\t\t"+tempTo+"\r\n\r\n";
                bufferTemp.append(temp);
                
            }
            bufferedWriter.write(bufferTemp.toString());
            bufferedWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(bufferedReader!=null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            if(bufferedWriter!=null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
    
    /*
     * toUpper
     * */
    private static boolean toUpper(String path,String topath) {
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            File file=new File(path);
            if(!file.exists()) {
                System.out.println("文件不存在《《《《《《"+path);
                return false;
            }
            File toFile=new File(topath);
            if(toFile.exists()) {
                toFile.delete();
            }
            toFile.createNewFile();
            bufferedReader=
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream(file),
                                    "GBK"));
            bufferedWriter=
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    new FileOutputStream(toFile,true), "GBK"));
            int count=0;
            StringBuffer bufferTemp=new StringBuffer();
            String str="";
            String temp="";
            String tempTo="";
            while((str=bufferedReader.readLine())!=null) {
                count++;
                temp=str.replaceAll("\\s*","");
                tempTo=temp.toUpperCase();
                if(!checkLength(temp,tempTo)) {
                    System.out.println("第"+count+"行出现错误《《《《《《《"+temp);
                }
                temp=count+"\t\t"+tempTo+"\r\n\r\n";
                bufferTemp.append(temp);
                
            }
            bufferedWriter.write(bufferTemp.toString());
            bufferedWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(bufferedReader!=null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            if(bufferedWriter!=null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
    
    /*
     * checklength between str1 and str2
     * */
    private static boolean checkLength(String str1,String str2){
        if(str1.length()==str2.length())
            return true;
        else
            return false;
    }
    
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于基于I/O复用的TCP回射服务器程序,我们可以在读入客户端发送的数据后,先对其进行大小写字母转换,再将转换后的数据发送回客户端即可。 具体实现步骤如下: 1.读取客户端发送的数据,可以使用select()函数进行I/O复用。 2.对读入的数据进行大小写字母转换,可以使用标准库中的toupper()和tolower()函数。 3.将转换后的数据发送回客户端。 下面是一个示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define MAX_LINE 1024 /* 最大数据长度 */ int main(int argc, char **argv) { int i, maxi, maxfd, listenfd, connfd, sockfd; int nready, client[FD_SETSIZE]; ssize_t n; fd_set rset, allset; char buf[MAX_LINE]; socklen_t cliaddr_len; struct sockaddr_in cliaddr, servaddr; char *str; /* 创建socket,即TCP套接字 */ listenfd = socket(AF_INET, SOCK_STREAM, 0); /* 绑定IP地址和端口号 */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(12345); bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); /* 监听 */ listen(listenfd, 20); /* 初始化 */ maxfd = listenfd; /* 初始化最大文件描述符 */ maxi = -1; /* 初始化客户端数组下标 */ for (i = 0; i < FD_SETSIZE; i++) client[i] = -1; /* 初始化客户端数组 */ FD_ZERO(&allset); /* 初始化文件描述符集 */ FD_SET(listenfd, &allset); /* 循环处理 */ for (;;) { rset = allset; nready = select(maxfd + 1, &rset, NULL, NULL, NULL); /* 处理新的连接 */ if (FD_ISSET(listenfd, &rset)) { cliaddr_len = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len); /* 将新的连接添加到客户端数组中 */ for (i = 0; i < FD_SETSIZE; i++) if (client[i] < 0) { client[i] = connfd; break; } if (i == FD_SETSIZE) { printf("too many clients"); exit(1); } /* 添加到文件描述符集中 */ FD_SET(connfd, &allset); if (connfd > maxfd) maxfd = connfd; if (i > maxi) maxi = i; /* 输出客户端IP地址和端口号 */ printf("new client: %s, port %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port)); /* 处理已经连接的客户端 */ for (i = 0; i <= maxi; i++) { if ((sockfd = client[i]) < 0) continue; if (FD_ISSET(sockfd, &rset)) { if ((n = read(sockfd, buf, MAX_LINE)) == 0) { /* 连接已经关闭 */ close(sockfd); FD_CLR(sockfd, &allset); client[i] = -1; } else { /* 数据处理 */ int j; for (j = 0; j < n; j++) { buf[j] = isupper(buf[j]) ? tolower(buf[j]) : toupper(buf[j]); //大小写字母转换 } write(sockfd, buf, n); } if (--nready <= 0) break; } } } } } ``` 以上代码实现了一个基于I/O复用的TCP回射服务器程序,并且在客户端发送数据后,对其进行大小写字母转换后再发送回客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值