Tag: 文件

Matrix转换为JTable

我需要使用静态2D数组填充JTable。 我为JTable创建了这个模型: public class InsertMatToJTable extends AbstractTableModel{ String titre[] = {“age real”, “sex real”, “chest real”, “resting_blood_pressure real”,”serum_cholestoral real”,”fasting_blood_sugar real”,”resting_electrocardiographic_results real”,”maximum_heart_rate_achieved real”,”exercise_induced_angina real”,”oldpeak real”,”slope real”,”number_of_major_vessels real”,”thal real”, “class”}; String line; float mat[][]= new float[270][13]; float matrice_normalise[][]; int i = 0,j=0; public void InsertMatToJTable() { try { FileInputStream fis = new FileInputStream(“fichier.txt”); BufferedReader br = new […]

为什么我找不到我的档案?

我正在尝试打开CSV文件名“logger.csv”,我已将其保存在源文件夹中。 public static void main(String[] args) { String filename = “logger.csv”; File motor_readings = new File(filename); try { Scanner inputStream = new Scanner(motor_readings); while (inputStream.hasNext()){ System.out.println(inputStream.next()); } inputStream.close(); } catch (FileNotFoundException e) { System.out.println(“Error: File not found!”); } } 但是,这继续给我一个“找不到文件”错误。

使用RandomAccessFile清除Java中的文件内容

我试图清除我在java中创建的文件的内容。 该文件由PrintWriter调用创建。 我在这里读到可以使用RandomAccessFile这样做,并在其他地方读取,这实际上比调用新的PrintWriter并立即关闭它以使用空白覆盖文件更好。 但是,使用RandomAccessFile不起作用,我不明白为什么。 这是我的代码的基本大纲。 PrintWriter writer = new PrintWriter(“temp”,”UTF-8″); while (condition) { writer.println(“Example text”); if (clearCondition) { new RandomAccessFile(“temp”,”rw”).setLength(0); // Although the solution in the link above did not include ‘,”rw”‘ // My compiler would not accept without a second parameter writer.println(“Text to be written onto the first line of temp file”); } } […]

有没有办法在java中的属性文件中获取某些过滤键的列表?

想象一下,你有这些属性文件“列表”: this.is.a.key.1 = lk1mldk3ml2k3e2l3k this.is.a.key.2 = 2309420398402931 this.is.a.key.3 = lksl1k2ml1kmd 所以… 有没有办法从属性文件中“提取”所有this.is.a.key。*键而不必逐个硬编码? 我问这个的原因是因为我不想要这个: this.is.a.list.key = 12983798123,82193819mx91,23891293819283,3892938492834 因为我需要键名中的那些“1,2,3 ……”名称来区分一个键而不是另一个键。 有解决方案吗 或者我必须硬编码?

附加zip存档调试

所以我有兴趣将文件附加到zip存档,我遇到了一些之前问过这个问题的用户,而另一个用户给出了这段代码片段作为该问题的解决方案: public static void updateZip(File source, File[] files, String path){ try{ File tmpZip = File.createTempFile(source.getName(), null); tmpZip.delete(); if(!source.renameTo(tmpZip)){ throw new Exception(“Could not make temp file (” + source.getName() + “)”); } byte[] buffer = new byte[4096]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source)); for(int i = 0; i -1; read […]

无法通过URL访问.jar中的文件

我需要使用URL访问当前执行的.jar中的文件。 URL url = BlockConverter.class.getResource(“/test.txt”); System.out.println(url.toString()); InputStream is = url.openStream(); 这就是我所做的。 输出是: jar:file:/C:/Users/User/Desktop/SERVER/plugins/MyJar.jar!/test.txt 我的InputStream在初始化时总是会抛出IOException,即使正确输出了URL。 那怎么可能呢? 为什么我不能打开流? 编辑:另外,请不要回答“使用getResourceAsStream”,因为它使用相同类型的代码: public InputStream getResourceAsStream(String name) { URL url = getResource(name); try { return url != null ? url.openStream() : null; } catch (IOException e) { return null; } }

虽然我实现了’Serializable’,但为什么我得到NotSerializableExceptionexception

我在类’userInfo’中实现了Serializable。 我仍然有例外。 这是输出控制台 – java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: chatservernazmus.User at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at java.util.ArrayList.readObject(ArrayList.java:593) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at chatservernazmus.Server.getAllUser(Server.java:74) at chatservernazmus.Server.(Server.java:44) at chatservernazmus.Main.main(Main.java:11) 这是我的两个class级: public class userInfo implements Serializable{ public […]

将文件(图像)从CacheDir复制到SD卡

我希望能够从Android设备的内部缓存移动或复制文件,并将其放入SD卡的永久存储中。 这是我到目前为止: public void onClickSaveSecret(View v){ File image = new File(getApplication().getCacheDir() + “/image.png”); File newImage = new File(Environment.getExternalStorageDirectory() + “/image.png”); Toast.makeText(this, “Image Saved”, 100).show(); }

未报告的exceptionjava.io.FileNotFoundException;?

我想打开一个文件并扫描它来打印它的令牌,但我收到错误:未报告的exceptionjava.io.FileNotFoundException; 必须被捕获或声明被抛出Scanner stdin = new Scanner(file1); 该文件位于具有正确名称的同一文件夹中。 import java.util.Scanner; import java.io.File; public class myzips { public static void main(String[] args) { File file1 = new File (“zips.txt”); Scanner stdin = new Scanner (file1); String str = stdin.next(); System.out.println(str); } }

在JAR中写入文件方法

我正在使用此方法从JAR中的文本文件中读取并正确工作。 BufferedReader Bbr=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(“AllBookRecords.txt”))); 现在,我想写入一个文件(从文件中删除一行,通过从第一个文件写入第二个文件而没有特定的行)。 我使用此代码执行此操作: String Bookid=tf1.getText(); File Tf=new File(“Boutput.txt”); URL myUrl=getClass().getResource(“AllBookRecords.txt”); File file=new File(myUrl.toURI()); OutputStream myoutput=new FileOutputStream(file); PrintWriter pw2=new PrintWriter(myoutput); String Bs; while( (Bs=Bbr.readLine()) != null ){ String[] Ust=Bs.split(” “); String Bname=Ust[0]; String Bdate=Ust[1]; String id=Ust[2]; if(!id.equals(Bookid.trim())){ pw2.println(Bs); } } pw2.close(); Bbr.close(); file.delete(); Tf.renameTo(file); } catch(FileNotFoundException fnfe){ foundlable.setText(“File Not Found”); } catch(IOException […]