Java将文本文件转换为逗号分隔文件
我有一个如下所示的文本文件:Java将文本文件转换为逗号分隔文件,java,file,Java,File,我有一个如下所示的文本文件: ExampleA1 ExampleA1b ExampleA2 ExampleA2b ExampleA3 ExampleA3b 有人能帮我把它转换成逗号分隔的格式吗 ExampleA1, ExampleA1b ExampleA2, ExampleA2b ExampleA3, ExampleA3b 谢谢你的帮助我可能会用到这样的东西 File f = new File("temp/file.txt"); if (!f.exists()) { return;
ExampleA1
ExampleA1b
ExampleA2
ExampleA2b
ExampleA3
ExampleA3b
有人能帮我把它转换成逗号分隔的格式吗
ExampleA1, ExampleA1b
ExampleA2, ExampleA2b
ExampleA3, ExampleA3b
谢谢你的帮助我可能会用到这样的东西
File f = new File("temp/file.txt");
if (!f.exists()) {
return;
}
Scanner scanner = null;
List<String> al = new ArrayList<String>();
try {
scanner = new Scanner(f);
while (scanner.hasNextLine()) {
String line1 = scanner.nextLine().trim();
if (line1.length() == 0) {
continue;
}
String line2 = "";
if (scanner.hasNextLine()) {
line2 = scanner.nextLine().trim();
}
if (line2.trim().length() > 0) {
al.add(line1 + ", " + line2);
} else {
al.add(line1);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scanner.close();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f, false);
PrintWriter pw = new PrintWriter(fos);
for (String str : al) {
pw.println(str);
}
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
我从上面得到这个
ExampleA1, ExampleA1b
ExampleA2, ExampleA2b
ExampleA3, ExampleA3b
乍一看,这项工作似乎很容易,但仔细一看就不那么容易了:)答案如下:
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("/home/venkatesh/Desktop/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
int data = -1;
int prevChar = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
do {
data = reader.read();
if (data == -1) break;
if (data == '\n') {
if (prevChar == '\n') {
baos.write('\n');
prevChar = 0;
} else {
prevChar = data;
}
} else {
if (prevChar == '\n') {
baos.write(',');
baos.write(' ');
}
baos.write(data);
prevChar = data;
}
} while (true);
System.out.println(" Written Text : \n" + new String(baos.toByteArray()));
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
简要说明:这里我们正在执行以下操作:
1。通过FileInputStream打开文件,然后由所选的读取器将其环绕。打开一个Outpustream(我在这里使用ByteArrayOutputStream来简化日志记录)。
2。使用Read()API逐个读取字符,并检查一些特定于应用程序的条件-
a。如果currentReadChar是新行字符(\n),并且以前没有找到新行字符,只需跳转到下一个字符(不向输出流写入任何内容)
b。如果currentReadChar是换行符,而previous也是换行符,则写一个换行符而不是两个换行符,然后继续下一个换行符。
c。如果currentReadChar不是一个换行符,而previous是一个换行符,那么在写入当前字符之前,先将“”写入流
d。如果currentReadChar不是换行符,而前一个也不是换行符,请写入当前字符。
3。关闭流/读卡器,并使用生成的outputstream/string(无论您想要什么)
基本假设:
1。ExampleA1和ExampleA1b之间有一个新行字符
2。在下一组示例A之间有两个新行字符。。。
希望这有帮助…
有人能帮我一下吗
,好的。你试过什么?我的意思是,除了问我们,先看一下,试着读入文本文件并打印出来,然后开始操作它。
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("/home/venkatesh/Desktop/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
int data = -1;
int prevChar = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
do {
data = reader.read();
if (data == -1) break;
if (data == '\n') {
if (prevChar == '\n') {
baos.write('\n');
prevChar = 0;
} else {
prevChar = data;
}
} else {
if (prevChar == '\n') {
baos.write(',');
baos.write(' ');
}
baos.write(data);
prevChar = data;
}
} while (true);
System.out.println(" Written Text : \n" + new String(baos.toByteArray()));
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}