D21

来源:互联网 发布:网络禁书100本百度云 编辑:程序博客网 时间:2024/04/18 22:04

1、编码:  String -- byte[]  * 解码: byte[] -- String

                String s = "你好";
// String -- byte[]
byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
// byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
// byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
System.out.println(Arrays.toString(bys));

---------------------------------------------------------------------------------------------------
// byte[] -- String
String ss = new String(bys); // 你好
// String ss = new String(bys, "GBK"); // 你好
// String ss = new String(bys, "UTF-8"); // ???
System.out.println(ss);

2、把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

public static void main(String[] args) throws IOException {// 封装数据源InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));// 封装目的地OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"));// 读写数据// 方式1// int ch = 0;// while ((ch = isr.read()) != -1) {// osw.write(ch);// }// 方式2char[] chs = new char[1024];int len = 0;while ((len = isr.read(chs)) != -1) {osw.write(chs, 0, len);// osw.flush();}// 释放资源osw.close();isr.close();}
public static void main(String[] args) throws IOException {// 封装数据源FileReader fr = new FileReader("a.txt");// 封装目的地FileWriter fw = new FileWriter("b.txt");// 一次一个字符// int ch = 0;// while ((ch = fr.read()) != -1) {// fw.write(ch);// }// 一次一个字符数组char[] chs = new char[1024];int len = 0;while ((len = fr.read(chs)) != -1) {fw.write(chs, 0, len);fw.flush();}// 释放资源fw.close();fr.close();}

public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("a.txt"));// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));// 两种方式其中的一种一次读写一个字符数组char[] chs = new char[1024];int len = 0;while ((len = br.read(chs)) != -1) {bw.write(chs, 0, len);bw.flush();}// 释放资源bw.close();br.close();}
public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("a.txt"));// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));// 读写数据String line = null;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();bw.flush();}// 释放资源bw.close();br.close();}

3、把c:\\a.txt内容复制到d:\\b.txt中

public static void main(String[] args) throws IOException {// 封装数据源FileReader fr = new FileReader("c:\\a.txt");// 封装目的地FileWriter fw = new FileWriter("d:\\b.txt");// 读写数据// int ch = 0;int ch;while ((ch = fr.read()) != -1) {fw.write(ch);}//释放资源fw.close();fr.close();}

4、需求:我有一个文本文件中存储了几个名称,请大家写一个程序实现随机获取一个人的名字。

public static void main(String[] args) throws IOException {// 把文本文件中的数据存储到集合中BufferedReader br = new BufferedReader(new FileReader("b.txt"));ArrayList<String> array = new ArrayList<String>();String line = null;while ((line = br.readLine()) != null) {array.add(line);}br.close();// 随机产生一个索引Random r = new Random();int index = r.nextInt(array.size());// 根据该索引获取一个值String name = array.get(index);System.out.println("该幸运者是:" + name);}
5、需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合

public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("b.txt"));// 封装目的地(创建集合对象)ArrayList<String> array = new ArrayList<String>();// 读取数据存储到集合中String line = null;while ((line = br.readLine()) != null) {array.add(line);}// 释放资源br.close();// 遍历集合for (String s : array) {System.out.println(s);}}

6、需求:把ArrayList集合中的字符串数据存储到文本文件

public static void main(String[] args) throws IOException {// 封装数据与(创建集合对象)ArrayList<String> array = new ArrayList<String>();array.add("hello");array.add("world");array.add("java");// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));// 遍历集合for (String s : array) {// 写数据bw.write(s);bw.newLine();bw.flush();}// 释放资源bw.close();}

7、需求:复制单极文件夹

public class CopyFolderDemo {public static void main(String[] args) throws IOException {// 封装目录File srcFolder = new File("e:\\demo");// 封装目的地File destFolder = new File("e:\\test");// 如果目的地文件夹不存在,就创建if (!destFolder.exists()) {destFolder.mkdir();}// 获取该目录下的所有文本的File数组File[] fileArray = srcFolder.listFiles();// 遍历该File数组,得到每一个File对象for (File file : fileArray) {// System.out.println(file);// 数据源:e:\\demo\\e.mp3// 目的地:e:\\test\\e.mp3String name = file.getName(); // e.mp3File newFile = new File(destFolder, name); // e:\\test\\e.mp3copyFile(file, newFile);}}private static void copyFile(File file, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}}
8、 需求:复制指定目录下的指定文件,并修改后缀名。

public class CopyFolderDemo {public static void main(String[] args) throws IOException {// 封装目录File srcFolder = new File("e:\\java");// 封装目的地File destFolder = new File("e:\\jad");// 如果目的地目录不存在,就创建if (!destFolder.exists()) {destFolder.mkdir();}// 获取该目录下的java文件的File数组File[] fileArray = srcFolder.listFiles(new FilenameFilter() {@Overridepublic boolean accept(File dir, String name) {return new File(dir, name).isFile() && name.endsWith(".java");}});// 遍历该File数组,得到每一个File对象for (File file : fileArray) {// System.out.println(file);// 数据源:e:\java\DataTypeDemo.java// 目的地:e:\\jad\DataTypeDemo.javaString name = file.getName();File newFile = new File(destFolder, name);copyFile(file, newFile);}// 在目的地目录下改名File[] destFileArray = destFolder.listFiles();for (File destFile : destFileArray) {// System.out.println(destFile);// e:\jad\DataTypeDemo.java// e:\\jad\\DataTypeDemo.jadString name =destFile.getName(); //DataTypeDemo.javaString newName = name.replace(".java", ".jad");//DataTypeDemo.jadFile newFile = new File(destFolder,newName);destFile.renameTo(newFile);}}private static void copyFile(File file, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}}

9、 * 需求:复制多极文件夹

public class CopyFoldersDemo {public static void main(String[] args) throws IOException {// 封装数据源FileFile srcFile = new File("E:\\JavaSE\\day21\\code\\demos");// 封装目的地FileFile destFile = new File("E:\\");// 复制文件夹的功能copyFolder(srcFile, destFile);}private static void copyFolder(File srcFile, File destFile)throws IOException {// 判断该File是文件夹还是文件if (srcFile.isDirectory()) {// 文件夹File newFolder = new File(destFile, srcFile.getName());newFolder.mkdir();// 获取该File对象下的所有文件或者文件夹File对象File[] fileArray = srcFile.listFiles();for (File file : fileArray) {copyFolder(file, newFolder);}} else {// 文件File newFile = new File(destFile, srcFile.getName());copyFile(srcFile, newFile);}}private static void copyFile(File srcFile, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}}
10、键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
public class StudentDemo {public static void main(String[] args) throws IOException {// 创建集合对象TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s2.getSum() - s1.getSum();int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName()): num4;return num5;}});// 键盘录入学生信息存储到集合for (int x = 1; x <= 5; x++) {Scanner sc = new Scanner(System.in);System.out.println("请录入第" + x + "个的学习信息");System.out.println("姓名:");String name = sc.nextLine();System.out.println("语文成绩:");int chinese = sc.nextInt();System.out.println("数学成绩:");int math = sc.nextInt();System.out.println("英语成绩:");int english = sc.nextInt();// 创建学生对象Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);// 把学生信息添加到集合ts.add(s);}// 遍历集合,把数据写到文本文件BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));bw.write("学生信息如下:");bw.newLine();bw.flush();bw.write("姓名,语文成绩,数学成绩,英语成绩");bw.newLine();bw.flush();for (Student s : ts) {StringBuilder sb = new StringBuilder();sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish());bw.write(sb.toString());bw.newLine();bw.flush();}// 释放资源bw.close();System.out.println("学习信息存储完毕");}}
11、已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl” 请编写程序读取数据内容,把数据排序后写入ss.txt中。

public static void main(String[] args) throws IOException {// 读取该文件的内容,存储到一个字符串中BufferedReader br = new BufferedReader(new FileReader("s.txt"));String line = br.readLine();br.close();// 把字符串转换为字符数组char[] chs = line.toCharArray();// 对字符数组进行排序Arrays.sort(chs);// 把排序后的字符数组转换为字符串String s = new String(chs);// 把字符串再次写入ss.txt中BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));bw.write(s);bw.newLine();bw.flush();bw.close();}







0 0
原创粉丝点击