想知道是否有人可以帮助弄清楚为什么这不想在Linux上工作。 导致这个更新程序适用于Mac,Linux和Windows的游戏,所以试图让每个人都可以毫无问题地使用它。
该程序在Windows上完美运行,没有任何错误。 我找到了我的朋友,使用Linux运行它所需的一切,它只是检测文件夹并停止。
package mu;
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class Main extends JFrame
{
private static final long serialVersionUID = 2627728992434582295L;
public String site;
public String filename;
public static String[] mods = new String[99];
public static String[] Cver = new String[99]; //Current Version
public static String[] webs = new String[99]; //Web Address to file
public static String[] Nver = new String[99]; //New Version
public static int num = 0;
public static final void main(String[] args) throws Exception
{
folders();
}
public static void folders(){
File dir = new File("").getAbsoluteFile();
File[] files = dir.listFiles();
FileFilter filefilter= new FileFilter(){
public boolean accept(File file){
return file.isDirectory();
}
};
files = dir.listFiles(filefilter);
System.out.println(files.length + " Mods found!");
if(files.length == 0){
}else{
for(int i=0; i<files.length; i++){
File filename = files[i];
Freader(filename);
}
}
}
public static void Freader(File dir){
File f = new File(dir + "\\version.txt");
if(f.exists()){
try{
FileReader in = new FileReader(f);
BufferedReader br = new BufferedReader(in);
String str;
int line = 0;
while((str = br.readLine()) != null){
if(line == 0){
mods[num] = str;
System.out.println(mods[num]);
}else if(line == 1){
Cver[num] = str;
System.out.println(Cver[num]);
}else if(line == 2){
webs[num] = str;
System.out.println(webs[num]);
Oreader(webs[num]);
}
line++;
}
num++;
br.close();
}catch(IOException e){
e.printStackTrace();
}
}else{
}
}
public static void Oreader(String dir){
try{
URL url = new URL(dir);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
int line = 0;
boolean newVer = false;
while((str = in.readLine()) != null){
if (line == 0){
Nver[num] = str;
if(!Cver[num].equals(Nver[num])){
System.out.println(Nver[num]);
System.out.println("New version available!");
newVer = true;
}
}else if(newVer && line == 1){
DL(str, (mods[num]+Nver[num].toString() + ".zip"));
}
line++;
}
in.close();
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public static void DL(String web,String name){
File dir = new File("").getAbsoluteFile();
float Precent = 0;
JFrame frm = new JFrame();
JProgressBar current = new JProgressBar(0,100);
current.setSize(394,25);
current.setValue(0);
current.setStringPainted(true);
frm.setTitle("Mod Updater");
frm.add(current);
JLabel label1 = new JLabel(mods[num],JLabel.CENTER);
frm.add(label1).setBounds(0, 10, 394, 50);
frm.setSize(400,100);
frm.setLayout(null);
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frm.setResizable(false);
frm.setVisible(true);
String site = web;
try{
URL url = new URL(site);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead=0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(name);
java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0){
totalDataRead=totalDataRead+i;
bout.write(data,0,i);
Precent=(totalDataRead*100)/filesize;
current.setValue((int)Precent);
if(Precent == 100){
extract(dir + "\\" +name);
}
}
bout.close();
in.close();
}catch(Exception e){
javax.swing.JOptionPane.showConfirmDialog((Component)null,e.getMessage(),"Error", javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
public static void extract(String filePath){
File dir = new File("").getAbsoluteFile();
FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
boolean dirs = false;
try {
fis = new FileInputStream(filePath);
zipIs = new ZipInputStream(new BufferedInputStream(fis));
while((zEntry = zipIs.getNextEntry()) != null){
try{
byte[] tmp = new byte[4*1024];
FileOutputStream fos = null;
String opFilePath = dir +"/"+zEntry.getName();
if(zEntry.isDirectory()){
dirs = new File(zEntry.getName()).mkdirs();
}else{
System.out.println("Extracting file to "+opFilePath);
fos = new FileOutputStream(opFilePath);
int size = 0;
while((size = zipIs.read(tmp)) != -1){
fos.write(tmp, 0 , size);
}
}
fos.flush();
fos.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
zipIs.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished");
}
}
答案 0 :(得分:1)
我怀疑你的问题就在这里:
extract(dir + "\\" +name);
据我所知,Linux并没有在它的路径中识别出反斜杠。另一方面,Windows确实识别正斜杠。此外,双斜线是多余的。尝试用'/'替换该位。
更好的是,正如@Teeg建议的那样,使用File.pathSeparator()
来获得真正的跨平台解决方案。以下是他的一句话:
此外,请勿使用硬编码路径分隔符 File.pathSeparator(),甚至更好的新文件(somePath,filename)
但这只是猜测,我们需要更多关于您要解决的问题的信息。
编辑:发现其他一些带有错误斜线的地方。不会列出所有人。搜索和替换。