请帮忙编写一个数据转换的java程序。。。

2024-12-04 00:18:16
推荐回答(3个)
回答(1):

FileWindow:
import java.io.File;
import javax.swing.JFileChooser;

public class FileWindow extends JFileChooser {

private static final long serialVersionUID = 1L;

private String defaultSavepath = "F:/JtestFile";

public FileWindow() {

}

public File load() {

File file = null;
this.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal;
returnVal = this.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = this.getSelectedFile();
}

return file;
}

public File save() {

File file = null;
file = new File(this.getDefaultSavepath());
if (!file.exists()) {
file.mkdir();
}
this.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.setCurrentDirectory(file);
this.showSaveDialog(null);
file = this.getSelectedFile();
return file;
}

public String getDefaultSavepath() {
return defaultSavepath;
}

public void setDefaultSavepath(String defaultSavepath) {
this.defaultSavepath = defaultSavepath;
}

}

TextReader:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TextReader {

private BufferedReader breader = null;

private FileReader freader = null;

public TextReader(File file) {
init(file);
}

public BufferedReader getBufreredReader() {
return breader;
}

private void init(File file) {

try {
freader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (freader != null) {
breader = new BufferedReader(freader);
}

}

public String readAll() {
StringBuffer result = new StringBuffer("");
String temp;
while ((temp = readLine()) != null) {
result.append(temp + "\n");
}
return result.toString();

}

public String readLine() {

String line = "";
try {
line = breader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}

public void close() {

if (breader != null) {
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (freader != null) {
try {
freader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
//用这个类转
DataCon:
public class DataCon {

public static void main(String[] args) {

StringBuffer to = new StringBuffer();
FileWindow fw = new FileWindow();
TextReader tr = new TextReader(fw.load());
String [] temp = new String[2];
String [] result = new String [2];
double iTemp;
String line = "";
while((line = tr.readLine()) != null) {
temp = line.split(",\\s*");
iTemp = Double.parseDouble(temp [0].trim());
if(iTemp >= 0 && iTemp <= 10) {
result [0] = 1 + ",";
} else {
result [0] = 0 + ",";
}
if("given".equals(temp[1].trim())) {
result [1] = 0 +"";
} else {
result [1] = 1 + "";
}
to.append(result[0]);
to.append(result[1] + "\n");
System.out.println(temp[0] + ": " + temp[1]);

}
System.out.println(to.toString());

}

}

回答(2):

20.8,given
30.4, given
1.24, not_given
11.3, given
5.00,given
7.33, not_given

这样的数据,你有文件吗。如果是个文件就简单些!
如果有文件的话,格式如你所写的这样,那么这还简单!

基本思路,io流读文件,把行转成字符串,对字符串进行split();
得到字符串数组的第一个字符串转double型数据,第2个进行equals判断!
组合数组。
然后写成文件!

回答(3):

刚刚不是提了一个相似的问题。
laogao3232 说的思路正确,如果一定要帮你写代码,把分我给,然后百度HI我一下,我再帮你写,很简单的事情。