java在线代码格式 java代码怎么写

如何在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怎么获取文件编码格式?没错,就这样做 , 文件格式和文件内容,不是这样的 。

推荐阅读