A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© baoxinyu 中级黑马   /  2016-10-13 22:04  /  1248 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.在本地e:/a.txt 并添加如下两行数据:
89,90,77,87,66,54,328,890,99
65,72,12,77,86,53,89

来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端

16 个回复

倒序浏览
。。。。。。。。。。
回复 使用道具 举报
public static void main(String [] args){
    File file =  new File("e:\\a.txt");
    FileOutputStream fos = new FileOutputStream(file);
     byte [] buffer = "89,90,77,87,66,54,328,890,99
                              65,72,12,77,86,53,89".getBytes();//直接复制过来就行回车也会在里面
     fos.write(buffer);
     fos.close();
}
这个好像是最基础的吧

点评

有问题吧,我感觉应该是添加而不是覆盖数据,所以在file后面加true更合适些  发表于 2016-10-15 14:17
回复 使用道具 举报
IceLoveInFire丶 发表于 2016-10-13 22:49
public static void main(String [] args){
    File file =  new File("e:\\a.txt");
    FileOutputStrea ...

好像有点复杂了吧 好多啊
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
回复 使用道具 举报

哥  完整题目是这样的


1.在本地E:/a.txt,并添加如下两行数据:
                  89,90,77,87,66,54,328,890,99
                  65,72,12,77,2,96,54,27,89
  
2.要求:编写程序读取此文件中的所有数字,并将重复的数字只保留一个写入另一个文件
回复 使用道具 举报
baoxinyu 发表于 2016-10-15 09:59
哥  完整题目是这样的

我说呢。
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
yyl010 初级黑马 2016-10-15 13:00:38
8#
int [] arr ={89,90,77,87,66,54,328,890,99};
                int [] arr1 = {65,72,12,77,2,96,54,27,89};
                BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\a.txt"));
                for (int i = 0; i < arr.length; i++) {
                        if(i==arr.length-1){
                                bw.write(arr[i]+"");
                        }else{
                               
                                bw.write(arr[i]+",");
                        }
                       
                       
                }
                bw.newLine();
                for (int i = 0; i < arr1.length; i++) {
                        if(i==arr1.length-1){
                                bw.write(arr1[i]+"");
                        }else{
                               
                                bw.write(arr1[i]+",");
                        }
                }
                bw.close();
               
                //编写程序读取此文件中的所有数字,并将重复的数字只保留一个写入另一个文件
                //1读取文件
                BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));
                BufferedWriter bw1 = new BufferedWriter(new FileWriter("d:\\b.txt"));
                TreeSet<Integer> ts = new TreeSet<>(); //除重
                String s;
                while ((s=br.readLine())!=null) {
                        String[] arr2=s.split(","); //变成字符串数组
                        int[] arr3 = new int[arr2.length];
                for (int i = 0; i < arr3.length; i++) {
                        arr3[i]=Integer.parseInt(arr2[i]);//变成数字数组
                        ts.add(arr3[i]);
                }       
                }
                System.out.println(ts);
                for (Integer in : ts) {
                        bw1.write(in+"  ");
                }
                br.close();
                bw1.close();
回复 使用道具 举报
IceLoveInFire丶 发表于 2016-10-13 22:49
public static void main(String [] args){
    File file =  new File(&quot;e:\\a.txt&quot;);
    FileOutputStrea ...

你这个byte数组,放不下890的
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
yyl010 发表于 2016-10-15 13:00
int [] arr ={89,90,77,87,66,54,328,890,99};
                int [] arr1 = {65,72,12,77,2,96,54,27,89};
                BufferedW ...

太感谢哥哥了
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
回复 使用道具 举报
z443863517 发表于 2016-10-15 13:02
你这个byte数组,放不下890的

对奥 我再重写一下。。
回复 使用道具 举报
z443863517 发表于 2016-10-15 13:02
你这个byte数组,放不下890的

大哥这个数组是装字节的 ,跟860没关系……差点被你忽悠了
来自宇宙超级黑马专属安卓客户端来自宇宙超级黑马专属安卓客户端
回复 使用道具 举报
IceLoveInFire丶 发表于 2016-10-13 22:49
public static void main(String [] args){
    File file =  new File("e:\\a.txt");
    FileOutputStrea ...

只关联一次文件,并不会覆盖
回复 使用道具 举报
一次读一行,用split(",")切割,存储到set集合去除重复,遍历写出到新文件
回复 使用道具 举报
* 1.在本地E:/numbers.txt,并添加如下两行数据: 89,90,77,87,66,54,328,890,99
* 65,72,12,77,2,96,54,27,89
* 2.要求:编写程序读取此文件中的所有数字,并将重复的数字只保留一个
* 写入另一个文件:E:/result.txt中 每个数字中间用逗号隔开;
* 要求: 1).最后一个数字后不能有逗号;

public static void main(String[] args) throws Exception {
                BufferedReader br = new BufferedReader(new FileReader("a.txt"));
                String s;
                ArrayList<String> list = new ArrayList<String>();
                HashSet<String> set = new HashSet<String>();
                while ((s = br.readLine()) != null) {
                        list.add(s);
                }
                br.close();
                for (String str : list) {
                        String[] ss = str.split(",");
                        for (String str2 : ss) {
                                set.add(str2);
                        }
                }
                FileOutputStream fos = new FileOutputStream("b.txt");
                int i = set.size();
                Iterator<String> it = set.iterator();
                while (it.hasNext()) {
                        if (i > 1) {
                                i--;
                                fos.write((it.next() + ",").getBytes());
                        } else {
                                fos.write((it.next()).getBytes());
                        }
                }
                fos.close();
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马