문제

I've a text file which contains text as:

VOLT=367
CURRENT=0.07
TEMP=031
RPM=3780
63HZ
VOLT=288
CURRENT=0.00
TEMP=030
RPM=3420
57HZ

and so on.... I want to take this text file as input in java and create an output text file having this text arranged as:

367,0.07,031,3780,63
288,0.00,030,3420,57

and so on until the end of txt file..

Coding attempt so far:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

try {
    FileInputStream fstream = new FileInputStream("file path\data.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    BufferedWriter brw = new BufferedWriter(new OutputStreamWriter(out));
    do {
        for (int i=1;i<50;i++) {
            //I dont know what to do here
            ...
도움이 되었습니까?

해결책

Try this,

String input = "";
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
StringBuilder result = new StringBuilder();
while ((input = br.readLine()) != null)
{
    if(input.contains("HZ"))
    {
        result.append(input.replace("HZ", ""));
        result.append("\n");
    }
    else
    {
        result.append(input.substring(input.indexOf("=") + 1, input.length()));
        result.append(",");
    }
}
System.out.println("result : "+result.toString());

다른 팁

Use this simple code.

String res="";
while ((input = br.readLine()) != null)
{
  if(input.indexOf("=")!= -1){
   res+=input.split("=+")[1]+",";
  }
  else{
    res+="\n";
  }
}
System.out.println("result : "+res.substring(0,res.length()-1));//To omit last ','
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top