一、保留两位数:
public static String formatTosepara(float data) {
DecimalFormat df = new DecimalFormat("#,###.00")
return df.format(data)
}
二、保留整数:
public static String formatTosepara(float data) {
DecimalFormat df =new DecimalFormat("#,###")
return df.format(data)
}
值得注意的一点是,在for循环的设置里头不应该设置i>=0,否则,当字符串长度是3的倍数时,会在字符串首位字符的前面多插入一个逗号。
这个是你要的么?
import java.io.BufferedReaderimport java.io.BufferedWriter
import java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
import java.io.RandomAccessFile
public class A {
static String file = "mytest.txt"
static String dfile = "mytest1.txt"
static int[] index = {9, 16, 18, 21, 23, 26, 40, 46, 53, 59, 66, 80}
public static void main(String[] args){
try {
prepare()
replace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public static void replace() throws IOException{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dfile))
BufferedReader bufferedReader = new BufferedReader(new FileReader(file))
String s = null
StringBuilder sb
while((s = bufferedReader.readLine())!=null){
sb = new StringBuilder()
sb.append(s)
for(int i = 0i<index.lengthi++){
sb.insert(index[i], ",")
}
bufferedWriter.append(sb.toString())
bufferedWriter.newLine()
}
bufferedWriter.flush()
bufferedWriter.close()
}
public static void prepare() throws IOException{
FileWriter fileWriter = new FileWriter(file)
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)
int i = 0
while(i<10000){
bufferedWriter.append("36202AAF4000006MSFSMD0000004263422034130000010333300000300000076550002"+String.format("%05d", i))
bufferedWriter.newLine()
i++
}
bufferedWriter.flush()
bufferedWriter.close()
}
}
具体的文件会生成在什么地方应该不用说了,lz你懂得
以下是打印的部分测试数据
36202AAF4,000006,M,SF,S,MD,0000004263422,03413,000001,03333,000003,0000007655000,20910136202AAF4,000006,M,SF,S,MD,0000004263422,03413,000001,03333,000003,0000007655000,209102
36202AAF4,000006,M,SF,S,MD,0000004263422,03413,000001,03333,000003,0000007655000,209103
36202AAF4,000006,M,SF,S,MD,0000004263422,03413,000001,03333,000003,0000007655000,209104
36202AAF4,000006,M,SF,S,MD,0000004263422,03413,000001,03333,000003,0000007655000,209105
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)