java之 21天 (三)"联通" 乱码 和 IO练习

本文探讨了UTF-8编码格式及其在Java中的应用,并实现了一个学生信息管理系统,该系统能够从键盘输入学生数据,计算总成绩,并按成绩排序后保存至文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要明白 UTF-8 编码 格式规则


/**
 * 记事本中的 联通的 编码问题
 *
 */
public class LiantongDemo {

	
	public static void main(String[] args) throws IOException {

		String s="联通";
		byte[] by=s.getBytes("gbk");
		System.out.println(Arrays.toString(by)); //[-63, -86, -51, -88]
		for (byte b : by) {
			System.out.println(Integer.toBinaryString(b&255)); //&255 是为了获取有效位
		}
		
		/*打印出来的 是 符合 UTF-8 编码格式.所以 记事本就开始使用  UTF-8 来编码了
		11000001   
		10101010
		11001101
		10101000*/
		System.out.println(new String(by,"UTF-8"));
	}

}



IO练习


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 有5个学生,每个学生有3门课程的成绩
 * 从键盘输入以上数据 包括 (姓名,3门 课程的成绩).
 * 输入的格式,如: 张三,30,40,60 计算出总成绩.
 * 并把学生的信息和计算出来的总分的高低顺序存放在磁盘文件stu.txt中
 * 
 * 思路:
 * 1.描述学生对象
 * 2.定义一个可以操作学生的工具类 
 * 
 * 思想:
 * 1.通过键盘录入一行数据,并将改行中的信息取出封装成学生对象.
 * 2.因为学生有很多,那么需要存储,使用到集合,因为要对学生的总分排序 
 *   所以可以使用TreeSet.
 * 3.将集合中的信息写入到一个文件中.
 */

class Student implements Comparable<Student>{
	private String name;
	private int ma,cn,en;
	private int sum;
	
	Student(String name,int ma,int cn,int en){
		this.name=name;
		this.ma=ma;
		this.cn=cn;
		this.en=en;
		sum=ma+cn+en;
	}
	
	public String getName(){
		return name;
	}
	public int getSum(){
		return sum;
	}
	
	public int hashCode(){
		return name.hashCode()+sum*2;
	}
	
	public boolean equals(Object obj){
		if(!(obj instanceof Student)){
			throw new RuntimeException("类型不匹配");
		}
		Student s=(Student)obj;
		return name.equals(s.name) && this.sum==s.sum;
	}
	
	
	public int compareTo(Student s){
		int num=new Integer(sum).compareTo(new Integer(s.sum));
		if(num==0)
			return this.name.compareTo(s.name);
		return num;
	}
	
	public String toString(){
		return "Student["+name+", "+ma+", "+cn+", "+en+"]";
	}
}

class StudentInfoTool{
	
	public static Set<Student> getStudents() throws Exception{
		return getStudents(null);
	}
	
	public static Set<Student> getStudents(Comparator<Student> cmp) throws Exception{
		BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
		
		String line=null;
		
		Set<Student> stus=null;
		if(cmp==null)
			stus=new TreeSet<Student>();
		else{
			stus=new TreeSet<Student>(cmp);
		}
		while((line=bufr.readLine())!=null){
			if("over".equals(line))
				break;
			
			String[] info=line.split(",");
			Student stu=new Student(info[0],
					new Integer(info[1]),
					new Integer(info[2]),
					new Integer(info[3])
					);
			stus.add(stu);
		}
		bufr.close();	
		return stus;
	}

	
	public static void write2File(Set<Student> stus) throws IOException{
		BufferedWriter bufw=new BufferedWriter(new FileWriter("stuinfo.txt"));
		
		for (Student stu : stus) {
			bufw.write(stu.toString()+"\t");
			bufw.write(stu.getSum()+"");
			bufw.newLine();
			bufw.flush();
		}
		bufw.close();
	}
	
	
}

public class StudyDemo {

	public static void main(String[] args) throws Exception {
		
		Comparator<Student> cmp=Collections.reverseOrder();  //使用比较器反转
		
		Set<Student>stus=StudentInfoTool.getStudents(cmp);
		
		StudentInfoTool.write2File(stus);

	}

}


您可以在导出Excel时,将双引号替换为&quot;,可以使用Java中的字符串替换方法来实现。以下是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExporter { public static void main(String[] args) { // 创建一个工作簿 Workbook workbook = new XSSFWorkbook(); // 创建一个工作表 Sheet sheet = workbook.createSheet(&quot;Sheet1&quot;); // 创建一个行 Row row = sheet.createRow(0); // 创建一个单元格,并设置值为带双引号的字符串 Cell cell = row.createCell(0); cell.setCellValue(&quot;Hello, \&quot;World\&quot;!&quot;); // 导出Excel时将双引号替换为&quot; String cellValue = cell.getStringCellValue().replace(&quot;\&quot;&quot;, &quot;&quot;&quot;); cell.setCellValue(cellValue); // 将工作簿写入文件 try (FileOutputStream outputStream = new FileOutputStream(&quot;output.xlsx&quot;)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } System.out.println(&quot;Excel导出成功!&quot;); } } ``` 在上述代码中,我们使用了 Apache POI 库来操作 Excel 文件。首先创建一个工作簿,然后创建一个工作表行,接着创建一个单元格,并设置它的值为带双引号的字符串。然后,使用 `replace` 方法将双引号替换为 `&quot;`,最后将工作簿写入文件中。 请注意,为了运行此代码,您需要在项目中添加 Apache POI 的相关依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值