-2
我想找到解決方案,爲什麼我的程序不能在Windows上工作。程序替換不工作在Windows上
我在Ubuntu 16.04
上測試過,一切正常。我的朋友在Mac
上進行了測試,結果也一樣 - 工作。
我的第一個想法是 - 錯誤的路徑。但是我們測試它的路徑是可以的。問題可能是Windows不會創建文件。我評論過其他代碼,並且txt甚至沒有創建。
我會很高興爲每個答案。對不起,我的英語和我的代碼:)
替換類:
package model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
*
* @author pepek
*/
public class Replace {
public static void replaceInFile(String fileName) throws IOException {
File file = new File(fileName);
// fileName = "/home/pepek/Desktop/Share/09a_pozice_asic_pcb.txt";
File tempFile = File.createTempFile("buffer", ".tmp");
FileWriter fw = new FileWriter(tempFile);
Reader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while(br.ready()) {
fw.write(br.readLine().replace(".", ",") +"\n");
}
fw.close();
br.close();
fr.close();
// Finally replace the original file.
tempFile.renameTo(file);
}
}
Okno類:
package gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.Replace;
/**
*
* @author pepek
*/
public class Okno extends JFrame {
private JButton btnGlass;
public Okno() {
setLayout(new GridLayout(0,2));
btnGlass = new JButton("Pozice skla");
btnGlass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\02_pozice_skla\\02_pozice_skla_data_new.txt");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnGlass);
JButton btnElement = new JButton("Pozice elementu");
btnElement.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\05_pozice_elementu\\05_pozice_elementu.TXT");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnElement);
JButton btnFireElement = new JButton("Vytvrzení elementu");
btnFireElement.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\07_pozice_elementu_vytvrdnuti\\07_pozice_elementu_vytvrdnuti_data.TXT");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnFireElement);
JButton btnUhelSpaceru = new JButton("Úhel spaceru");
btnUhelSpaceru.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\08_uhel_spaceru\\08_uhel_spaceru.TXT");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnUhelSpaceru);
JButton btnPozice = new JButton("Pozice ASIC a PCB");
btnPozice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\09a_pozice_asic_pcb\\09a_pozice_asic_pcb.TXT");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnPozice);
JButton btnVyska = new JButton("Výška ASIC a PCB");
btnVyska.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\10a_vyska_asic_pcb\\10a_vyska_asic_pcb.txt");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnVyska);
JButton btnVyskaSpaceru = new JButton("Vytvrzení elementu");
btnVyskaSpaceru.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Replace.replaceInFile("D:\\merove programy\\15_vyska_spaceru\\15_vyska_spaceru.txt");
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
add(btnVyskaSpaceru);
setTitle("Find&Replace App"); // "super" Frame sets its title
setSize(750, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Okno();
}
}
請在頁面上發佈您的代碼,不要鏈接它。 –
補充說明@PauloSoares –