如何用java实现不同编码方式字符串的转换(包含异常处理、重复不终止输入、缓冲区bufferedreader的使用)
来源:互联网 发布:python和java哪个好学 编辑:程序博客网 时间:2024/06/05 12:07
问题:java 实现不同编码方式的字符串的转换 ,在程序中实现异常处理、重复不终止输入,缓冲区bufferedreader的使用。
比如 “utf-8”,”utf-16”,”gbk”,”gb2312”,”iso-8859-1”,”ascii”的互相转换
部分代码
public static void main(String[] args) throws IOException, WrongStringException, WrongChoiceException { BufferedReader in=null; //缓冲区 String sTemp=""; //暂存转换的结果 int temp=-1; //存储退出的方式 0、1(重来)、-1 trans s=new trans(); //main中(static)必须要对类实例化 while(true){ //程序循环输入要转换的代码 if(temp==0) break; //实现反复输入,唯有temp==0才会从主循环退出 System.out.println("Plese input the string you want to transform"); try{ in=null; in =new BufferedReader(new InputStreamReader(System.in)); s.setAString(in.readLine()); //输入流 //唯有temp==0才会从主循环退出 } catch(IOException e){ System.out.println(e);} while(true){//输入选择哪种方法编码 if(temp==0||temp==1) break; System.out.println("Please input the [choice 1] numbers which 0-6 represent respectively default utf-8 utf-16 gbk gb2312 iso-8859-1 ascii"); try{ in=null; in =new BufferedReader(new InputStreamReader(System.in)); s.setEncodingSign(Integer.parseInt(in.readLine())); //输入流 s.stringEncoding(); //只有temp==0或者temp==1才会从循环退出 while(true){//输入选择哪种方法解码 if(temp==0||temp==1) break; //只有temp==0或者temp==1才会从循环退出 System.out.println("Please input the [choice 2] numbers which 0-6 represent respectively default utf-8 utf-16 gbk gb2312 iso-8859-1 ascii"); try { in=null; in =new BufferedReader(new InputStreamReader(System.in)); s.setDecodingSign(Integer.parseInt(in.readLine())); //输入流 } catch(IOException e){ System.out.println("choice number is error!");} //输出转换的结果 sTemp=s.stringDecoding(s.TranString); System.out.println("The result of the translation is:"+sTemp); while(true){//选择重来或退出 System.out.println("Please choose exit or again:"); try{ temp=s.ExitOrAgain(); if(temp==0||temp==1) break; //只有temp==0或者temp==1才会从循环退出 } catch(IOException e){ System.out.println("exit error!");} } } } catch(IOException e){ System.out.println("choice number is error!");} } } if(in!=null) try { in.close(); }//检查文件是否关闭 catch(IOException e){ System.out.println("文件关闭出错");} else{ System.out.print("退出成功!");//提示信息 System.exit(0); } }}
完整代码(包括实现类,异常处理)的源码
http://download.csdn.net/detail/immenselee/9801832
0 0
- 如何用java实现不同编码方式字符串的转换(包含异常处理、重复不终止输入、缓冲区bufferedreader的使用)
- Java中了使用list方式查看输入的字符串中不重复的字符
- 如何用递归的方式颠倒字符串
- 如何用不同的方式来处理安卓的权限管理?
- 字符串的不同输入方式
- 不同编码字符串的转换
- Java中字符串编码方式的转换
- 如何用C#生成不重复的随机数
- 如何用matlab生成不重复的随机整数矩阵
- 如何用arrayList不重复的存储实体类
- 输入一串不重复的字符串,列出所有可能的组合(java实现)
- 如何用java实现使用电子邮件控制你的电脑
- win下如何用cmd转换记事本的编码格式
- 使用BufferedReader实现整数、小数、字符串、日期类型数据的输入
- 如何用JAVA实现文件的覆盖
- 关于PHP如何用实现防止用户在浏览器上使用后退功能重复提交输入
- 字符串在不同编码间的转换
- c++和java总异常是如何用c语言实现的
- Cannot find a valid baseurl for repo: base/7/x86_64
- 虚拟机中linux系统无法打开原保存的显示器配置解决方法
- 最近的学习心得
- Oracle存储过程表名称列名称做参数,动态SQL
- JavaScript内置对象
- 如何用java实现不同编码方式字符串的转换(包含异常处理、重复不终止输入、缓冲区bufferedreader的使用)
- Angular4.0.0正式版发布
- python1-3月25日
- Java中HashMap源码浅析
- list遍历方式效率分析
- RecyclerView系列之(1):为RecyclerView添加Header和Footer
- 算法的时间和空间复杂度
- 详解js中的apply与call的用法
- hdu 2047 EOF 串