使用java获取dxdiag结果

时间:2014-05-18 14:19:16

标签: java

                String filePath = "./results.txt";

                // Use "dxdiag /t" variant to redirect output to a given file
                ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","dxdiag","/t",filePath);
                System.out.println("-- Executing dxdiag command --");
                Process p = pb.start();
                p.waitFor();

                BufferedReader br = new BufferedReader(new FileReader(filePath));
                String line;
                System.out.println(String.format("-- Printing %1$1s info --",filePath));
                while((line = br.readLine()) != null){

                    if(line.trim().startsWith("Card name:")
                       || line.trim().startsWith("Current Mode:")
                       || line.trim().startsWith("Display Memory:"))

                        textArea_3.append(line.trim() + "\n");

                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

我得到的错误是

java.io.FileNotFoundException: .\results.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at java.io.FileReader.<init>(FileReader.java:58)
at gui.Gui$4.actionPerformed(Gui.java:136)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

我已经在Win7(无法正常工作)和Win8(它按预期工作)上尝试过。如果有人想到会发生什么事,我非常感谢你的帮助,那就是。

2 个答案:

答案 0 :(得分:1)

这似乎是一个相对路径问题。我建议尝试添加一行System.out.println(new File(filePath).getAbsolutePath());在实例化缓冲的阅读器之前,看看系统究竟在哪里寻找结果文件。您可能还希望将DXDIAG的输出位置指定为绝对路径,以确保将结果存储在您希望的位置。

答案 1 :(得分:0)

第一个填写您的代码,您的班级名称丢失。

第二个给出主要方法public static void main(String args[])

第3 个尝试块。

最后一个第4个创建了您的代码中缺少的TextArea对象:TextArea textArea_3 = new TextArea();

代码将起作用。