Tag: io

如何检查和关闭excel文件是否已在java中打开

可能重复: Java:检查文件是否已打开 我正在制作一个swing实用程序,我在其中创建并使用Apache poi的excel表。 如果在我试图访问它时文件已经打开,它会抛出一个exception,就像其他进程正在使用这个excel文件一样。 所以我想要的是检查是否已打开excel文件,如果是,则关闭它。

如何将文本附加到Java中的现有文件中

我需要将文本重复附加到Java中的现有文件中。 我怎么做?

java.io.IOException:系统找不到指定的路径

我试图打开我刚刚在我的代码中创建的文件(所以我确信该文件存在) 代码是这样的: File file = new File(filename); file.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); … bw.close(); try { Desktop desktop = null; if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); } desktop.open(file); } catch (Exception e) { … } 但正如标题所示,我从desktop.open(文件)istruction获得“java.io.IOException:系统无法找到指定的路径”。 问题肯定是文件路径名包含空格(转换为“%20”)。 有没有办法避免这种情况?

未报告的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); } }

更新捆绑的资源文件

我正在做以下事情, String str = “this is the new string”; URL resourceUrl = getClass().getResource(“path_to_resource”); File file = new File(resourceUrl.toURI()); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(xml); writer.close(); 在上面的代码中,我试图写入我的一个java包中包含的资源文件。 执行代码后,我的程序执行正常,但文件只更新web-INF中的属性文件,而不是存储它的包。 任何人都可以帮我解释如何实现这一点或我在这里做错了什么? 非常感谢。

使用next()或nextFoo()后,Scanner正在跳过nextLine()?

我正在使用Scanner方法nextInt()和nextLine()来读取输入。 它看起来像这样: System.out.println(“Enter numerical value”); int option; option = input.nextInt(); // Read numerical value from input System.out.println(“Enter 1st string”); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println(“Enter 2nd string”); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) 问题是在输入数值后,跳过第一个input.nextLine()并执行第二个input.nextLine() ,这样我的输出如下所示: Enter numerical value 3 // […]

关闭system.in后的NoSuchElementException

[ 中断线程等待用户输入然后退出应用程序 ] 我想做与上面链接中提到的相同的练习。 但是,当我在成功关闭BufferedReader object之后尝试使用inConsole.nextLine()时,我遇到了NoSuchElementException ,尽管重新分配inConsole = new Scanner(System.in)以从标准输入中读取。 关闭BufferedReader object ,我不能像上面那样重新分配给Scanner对象并使用它吗?

如何使用文本文件中的PrinterWriter类实现以下结果?

我的应用程序在此处提示用户输入包含的文本文件mixed.txt 12.2 Andrew 22 Simon Sophie 33.33 10 Fred 21.21 Hank Candice 12.2222 接下来,应用程序是PrintWrite到所有文本文件,即result.txt和errorlog.txt 。 mixed.txt中的每一行都应以数字开头,后跟名称。 但是,某些行可能包含另一种方式来表示名称,然后是数字。 以数字开头的那些应加到sum变量并写入result.txt文件,而那些以名称开头的行和数字应写入errorlog.txt文件。 因此,在MS-DOS控制台上的结果如下: 输入result.txt 总计:65.41 输入errorlog.txt 第3行出错 – 索菲33.33 第6行出错 – Candice 12.2222 好的,这是我的问题。 我只是设法进入舞台,我已将所有数字添加到result.txt和errorlog.txt文件的名称,我不知道如何继续从那里开始。 那么你们能给我一些建议或帮助我们如何实现我需要的结果吗? 以下是我的代码: import java.util.*; import java.io.*; class FileReadingExercise3 { public static void main(String[] args) throws FileNotFoundException { Scanner userInput = new Scanner(System.in); Scanner fileInput […]

创建FileInputStream时出现java.io.FileNotFoundException

尝试打开FileInputStream以从.ser扩展名的文件加载Map时出错。 构造函数,我在其中创建新的File并调用从文件加载映射的方法: protected DriveatorImpl() { accounts = new ConcurrentHashMap(); db = new File(“database.ser”); // oddly this does not create a file if one does not exist loadDB(); } @SuppressWarnings(“unchecked”) private void loadDB() { try { fileIn = new FileInputStream(db); in = new ObjectInputStream(fileIn); accounts = (Map) in.readObject(); in.close(); fileIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); […]

Java – 了解PrintWriter并需要刷新

好的,首先我为所有代码道歉,但我觉得太多的代码比不够好。 我正在制作一个简单的聊天客户端和编剧,特别是我正在努力。 使用代码的方式现在它将与服务器类进行交互,并且非常精细并打印我想要打印的内容。 但是,当我删除’writer.flush();’ 它会停止打印。 凭借我的理解 – 这显然是错误的 – ‘writer.println(outgoing.getText());’ 应该足够了,因为这会发送我试图发送到服务器的文本。 我理解flush会强制写入所有信息,但为什么在我写完我想写的内容时需要这样做? public class SimpleChatClientA { JTextArea incoming; JTextField outgoing; BufferedReader reader; PrintWriter writer; Socket sock; public void go(){ JFrame frame = new JFrame(“Ludicrously Simple Chat Client”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); incoming = new JTextArea(15,50); incoming.setLineWrap(true); incoming.setWrapStyleWord(true); incoming.setEditable(false); JScrollPane qScroller = new JScrollPane(incoming); […]