public class SY63 { public static void main(String[] args) throws Exception { System.out.print("请输入字符串:"); Scanner in = new Scanner(System.in); String st = in.nextLine(); FileWriter fw = new FileWriter("d:\\test.txt"); fw.write(st); fw
在用二进制模式打开文件情况下,写入一个str对象时报错:TypeError: a bytes-like object is required, not 'str' 出现该问题是因为Python严格区分二进制和文本文件的操作,二进制文件打开模式下写入的对象类型不能是str类型,只能是bytes类型,解决办法非常的简单,就是将str转换成bytes类型,具体实现有两种方案: 用encode()方法将str类型转换成bytes类型: fp.write(fd,text.encode()) #text为要