【菜鸟求助】从一个txt向另一个txt打数据,用“,”分行

Gene1994 2016-12-28 05:53:33
刚刚自学JAVA,对类型之间的转换还是不是很明白。。
想从
执行后效果是
import java.io.*;


public class SongList {

FileInputStream in = null;
FileOutputStream out = null;
int b = 0;
public static void main(String[] args) {
SongList s = new SongList();
s.go();

}
public void go(){
try{
in = new FileInputStream("c:/songlist.txt");
out = new FileOutputStream("C:/songlist(1).txt");
while ((b = in.read()) != -1){
String song = in.read()+"";
String[] s =song.split(",");
String k = s.toString();
byte[] m = k.getBytes();
out.write(m);
}
System.out.println("succeed!");
}catch(FileNotFoundException fe){
System.out.println("not found");
}catch (IOException ie) {
System.out.println("IOException");
}
}
}


这是我写的。。
结果是


想请教下 我是不是哪儿块儿理解出问题了。。就是类型转换不对造成这种结果了么。。
还有的话 就是如果songlist.txt里面如果有中文的话是不是需要用FileReader 和FileWriter? 谢谢!
...全文
175 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 4 楼 Gene1994 的回复:
想问下 能不能用split()方法呀...想使用这个方法...
split()也可以实现
Gene1994 2016-12-29
  • 打赏
  • 举报
回复
想问下 能不能用split()方法呀...想使用这个方法...
Gene1994 2016-12-28
  • 打赏
  • 举报
回复
引用 2 楼 LinBilin_ 的回复:

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		readTxt("E:\\test\\test0.txt");
	}
	
	  /** 
     * 读取txt文本 
     *  
     * @param path 
     */  
    public static void readTxt(String path) {  
        System.out.println("读取的文件路径为:"+path);  
        File file = new File(path);  
        BufferedReader reader = null;  
        String tempString = null;  
        StringBuilder result = new StringBuilder();   
        try {  
            reader = new BufferedReader(new FileReader(file));  
            while ((tempString = reader.readLine()) != null) {  
                if (!tempString.isEmpty()) {  
                	result.append(tempString.replaceAll(",", "\r\n")).append("\r\n");
                }  
            }  
            writeText(result.toString(), "E:\\test\\test.txt");   
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
      
    /** 
     * 写入文件 
     * @param text 
     * @param path 
     */  
    public static void writeText(String text, String path) {  
        System.out.println("保存的文件路径为:"+path);  
        FileWriter fileWrite = null;  
        try {  
            fileWrite = new FileWriter(path);  
            fileWrite.write(text);  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fileWrite.flush();  
                fileWrite.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

}

cool! 好好看看!谢谢!
  • 打赏
  • 举报
回复

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		readTxt("E:\\test\\test0.txt");
	}
	
	  /** 
     * 读取txt文本 
     *  
     * @param path 
     */  
    public static void readTxt(String path) {  
        System.out.println("读取的文件路径为:"+path);  
        File file = new File(path);  
        BufferedReader reader = null;  
        String tempString = null;  
        StringBuilder result = new StringBuilder();   
        try {  
            reader = new BufferedReader(new FileReader(file));  
            while ((tempString = reader.readLine()) != null) {  
                if (!tempString.isEmpty()) {  
                	result.append(tempString.replaceAll(",", "\r\n")).append("\r\n");
                }  
            }  
            writeText(result.toString(), "E:\\test\\test.txt");   
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
      
    /** 
     * 写入文件 
     * @param text 
     * @param path 
     */  
    public static void writeText(String text, String path) {  
        System.out.println("保存的文件路径为:"+path);  
        FileWriter fileWrite = null;  
        try {  
            fileWrite = new FileWriter(path);  
            fileWrite.write(text);  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fileWrite.flush();  
                fileWrite.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

}

Gene1994 2016-12-28
  • 打赏
  • 举报
回复
感觉自己的code相当不“面向对象”

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧