Tag: file io

BufferedReader.ready()方法是否确保readLine()方法不返回NULL?

我有这样的代码使用BufferedReader读取文本文件: BufferedReader reader=null; try { reader = new BufferedReader(new FileReader(“file1.txt”)); while (reader.ready()) { final String line = reader.readLine(); System.out.println(“”); } catch (..) { … } 它工作正常,但Findbugs报告一个警告: NP_DEREFERENCE_OF_READLINE_VALUE:取消引用调用readLine()的结果,而不检查结果是否为null。 如果没有更多的文本行要读取,readLine()将返回null并解除引用将生成空指针exception。 当我将FileReader更改为StringReader ,即 BufferedReader reader=null; try { reader = new BufferedReader(new StringReader(“ABCD”)); while (reader.ready()) { final String line = reader.readLine(); System.out.println(“”); } catch (..) { … } readLine方法返回null而ready方法总是返回true […]

线程“main”中的exceptionjava.io.FileNotFoundException:错误

我正在使用Eclipse来编译和运行我的java代码。 这是我得到的错误。 Exception in thread “main” java.io.FileNotFoundException: file.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.util.Scanner.(Unknown Source) at helloworld.main(helloworld.java:9) 这是我的代码 import java.io.File; import java.io.IOException; import java.util.Scanner; public class helloworld { public static void main(String[] args) throws IOException { Scanner KB = new Scanner(new File(“file.txt”)); while (KB.hasNext()) { String […]

FileLock如何工作?

我一直在尝试使用FileLock获取对文件的独占访问权限,以便: 删除它 重命名 写信给它 因为在Windows上(至少),您似乎无法删除,重命名或写入已在使用的文件。 我写的代码看起来像这样: import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public abstract class LockedFileOperation { public void execute(File file) throws IOException { if (!file.exists()) { throw new FileNotFoundException(file.getAbsolutePath()); } FileChannel channel = new RandomAccessFile(file, “rw”).getChannel(); try { // Get an exclusive lock on the whole file FileLock […]

逐列读取CSV文件

我想从多列csv文件中读取特定列,并使用Java在其他csv文件中打印这些列。 有什么帮助吗? 以下是我的代码逐行打印每个标记..但我打算只打印多列csv中的几列。 import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.StringTokenizer; public class ParseCSV { public static void main(String[] args) { try { //csv file containing data String strFile = “C:\\Users\\rsaluja\\CMS_Evaluation\\Drupal_12_08_27.csv”; //create BufferedReader to read csv file BufferedReader br = new BufferedReader( new FileReader(strFile)); String strLine = “”; StringTokenizer st = null; int lineNumber = […]

Java读取文件并将文本存储在数组中

我知道如何使用Scanner和File IOException读取Java文件,但我唯一不知道的是如何将文本作为数组存储在文件中。 这是我的代码snippet : public static void main(String[] args) throws IOException{ // TODO code application logic here // // read KeyWestTemp.txt // create token1 String token1 = “”; // for-each loop for calculating heat index of May – October // create Scanner inFile1 Scanner inFile1 = new Scanner(new File(“KeyWestTemp.txt”)); // while loop while(inFile1.hasNext()){ // how […]

我想在eclipse中使用哪个文本文件?

我启动程序时需要读取文本文件。 我正在使用eclipse并启动了一个新的java项目。 在我的项目文件夹中,我得到了“src”文件夹和标准的“JRE系统库”+ staedteliste.txt …我只是不知道在哪里放文本文件。 我真的尝试了我能想到的每个文件夹….我不能使用“硬编码”路径,因为文本文件需要包含在我的应用程序中… 我使用以下代码来读取文件,但是我收到此错误: Error:java.io.FileNotFoundException:staedteliste.txt(No such file or directory) public class Test { ArrayList values; public static void main(String[] args) { // TODO Auto-generated method stub URL url = Test.class.getClassLoader().getResource(“src/mjb/staedteliste.txt”); System.out.println(url.getPath()); // I get a nullpointerexception here! loadList(); } public static void loadList() { BufferedReader reader; String zeile = null; try { […]

使用凭据将I / O文件写入共享网络驱动器

我想在共享网络驱动器上删除.txt文件。 该路径是网络驱动器上的映射,需要凭据(登录名和密码)。 我可以使用FileOutputStream传递这些参数吗? FileOutputStream fos; DataOutputStream dos; try { File file= new File(path + “/” + fileName + “.txt”); fos = new FileOutputStream(file); dos=new DataOutputStream(fos); dos.writeChars(stringContent); dos.close(); fos.close(); } catch(IOException eio){ } 谢谢。

写入控制台和文本文件

我从互联网上找到了下面的代码,但它不会将打印的控制台写入omt.txt,它只会在第二个catch块之后写入System.out.println语句。如果你运行代码,你会明白什么我的意思是。我想要的是将控制台上的内容写入“omt.txt”文件,这些文件都是…… 经过一些回答,我发现我的问题不明确,对不起。 我想将控制台输出保存到omt.txt文本文件中。 如果在控制台上打印“Hello 123”,它也应该在omt.txt文件中。换句话说,打印机上的任何内容都应该同时写在om.txt文件中,或者可以在控制台执行后但是应该是1对1相同! import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; public class Wrt_file { public static void main(String[] args) { System.out.println(“THIS is what I see on the console. but not on TEXT file”); File f = new File(“omt.txt”); if(!f.exists()) { try { f.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } try { FileOutputStream […]

Java字节数组包含负数

我正在将文件读入块中的字节数组,并通过网络服务器通过POST请求将其发送到网络服务器。 这并不复杂,我在使用这个完全相同的代码之前已经完成了。 这一次,我注意到我的图像在到达服务器时看起来很奇怪,所以我决定查看正在发送的字节数组和正在接收的字节数据以确保它是相同的。 不是。 在java发送端,字节数组包含负数。 在C#接收方,没有负数。 接收端的前15个字节(C#) 137 80 78 71 13 10 26 10 0 0 0 13 73 72 68 那些相同的字节,但在发送方(java) -119 80 78 71 13 10 26 10 0 0 0 13 73 72 68 所有的非负数都是相同的,-119不是唯一的负数,它们都已经结束了。 我注意到-119和137分别是256,并想知道这是否与它有关。 我用来读取图像的代码(java) public static byte[] readPart(String fileName, long offset, int length) throws FileNotFoundException, Exception { byte[] […]

如何从Java中的某个偏移量读取文件?

嘿我正在尝试打开一个文件并从偏移中读取一定长度! 我读了这个主题: 如何使用Java中的文件中的特定行号读取特定行? 在那里,它表示不能在不读取行之前读取某一行,但我想知道字节! FileReader location = new FileReader(file); BufferedReader inputFile = new BufferedReader(location); // Read from bytes 1000 to 2000 // Something like this inputFile.read(1000,2000); 是否可以从已知偏移中读取某些字节?