如何在Java代码中指定生成"Unix格式的文本文件“?写入文本文件时指定文件编码为gbk试试 。每行结尾换行都输入\r\n 。
java中GBK编码格式转成UTF8,用一段方法实现怎么做import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ConvertCharset
{
private static final String SEPARATOR = System.getProperty ("line.separator");
/**
* @param path the file path or directory
* @param back the backup file path or directory
* @param cs1 the orignle charset name
* @param cs2 the dest charset to convert
*/
public static void convertCharset ( String path, String back, String cs1, String cs2 )
{
try
{
File dir = new File (path);
// a directory, not a file
if (dir.isDirectory ())
{
File[] files = dir.listFiles ();
for ( int i = 0; ifiles.length; i++ )
{
File dirt = files[i];
String fn = dirt.getName ();
String name = path + File.separator + fn;
// recursion
if (dirt.isDirectory ())
{
convertCharset (name, back + File.separator + fn, cs1, cs2);
}
else
{
// write
File dest = new File(back);
if (!dest.exists ())
{
dest.mkdirs ();
}
FileOutputStream fos = new FileOutputStream (back + File.separator + fn);
OutputStreamWriter osw = new OutputStreamWriter (fos, cs2);
BufferedWriter bw = new BufferedWriter (osw);
// read
FileInputStream fis = new FileInputStream (name);
InputStreamReader isr = new InputStreamReader (fis, cs1);
BufferedReader br = new BufferedReader (isr);
String line = null;
while (null != ( line = br.readLine () ))
{
bw.write (line + SEPARATOR);
}
// write
fos.flush ();
osw.flush ();
bw.flush ();
bw.close ();
osw.close ();
fos.close ();
// read
br.close ();
isr.close ();
fis.close ();
}
}
}
// a single file
else
{
// write
File dest = new File(back);
if (!dest.exists ())
{
dest.mkdir ();
}
FileOutputStream fos = new FileOutputStream (back + File.separator + dir.getName ());
OutputStreamWriter osw = new OutputStreamWriter (fos, cs2);
BufferedWriter bw = new BufferedWriter (osw);
// read
FileInputStream fis = new FileInputStream (path);
InputStreamReader isr = new InputStreamReader (fis, cs1);
BufferedReader br = new BufferedReader (isr);
【java在线代码格式 java代码怎么写】String line = null;
while (null != ( line = br.readLine () ))
{
bw.write (line + SEPARATOR);
}
// write
fos.flush ();
osw.flush ();
bw.flush ();
bw.close ();
osw.close ();
fos.close ();
// read
br.close ();
isr.close ();
fis.close ();
}
System.out.println ("Convert Success !");
}
catch (Exception e)
{
e.printStackTrace ();
System.out.println ("Convert Failed !");
}
}
public static void main ( String[] args )
{
// a single file
convertCharset ("I:\\2013313workspace\\Yaojiangji\\src\\yaojiangji\\paixu.java",
"I:\\2013313workspace\\Yaojiangji\\back",
"GBK",
"UTF-8");
// not a file , it's a directory
//convertCharset ("I:\\2013313workspace\\Yaojiangji\\src\\yaojiangji",
//"I:\\2013313workspace\\Yaojiangji\\back\\yaojiangji",
//"GBK",
//"UTF-8");
}
}
java怎么获取文件编码格式?没错,就这样做 , 文件格式和文件内容,不是这样的 。
推荐阅读
- 装修直播技巧与话术技巧,装修公司直播讲什么话题
- 门店营销如何赋能营销技巧,门店营销如何赋能营销技巧和方法
- linux命令从起服务,linux所有服务的启动脚本
- 成都新媒体前景如何样知乎,成都新媒体运营
- go语言的内存管理 go语言实现简单kv存储
- linux命令马上生效,linux执行命令时间
- 肉夹馍加盟开店如何做推广,肉夹馍加盟费用是多少
- 婴儿拍摄容器是什么,婴儿拍摄容器是什么东西
- vb.net获取当前日期 vba 获取当前日期