嗨,谢谢你抽出时间来看我的问题。
我正在构建一个程序,它递归地搜索一个目录,然后显示该目录中最大文件的名称。在第一步,程序似乎运行良好,但如果我进入一个新的路径,并搜索第二次程序抛出StackOverflowError。
首先:我对这个问题的研究告诉我,有一个无限循环很可能是由递归方面创建的,但是对于我的生活,我不知道它发生在哪里,为什么会发生,以及如何防止它发生。
第二:如果这很重要,我将使用测试目录来测试程序,并在测试目录中包含不同大小的其他文件夹和文本文件。当我第一次运行这个程序时,它会显示正确的文件名。
我遇到了一个完全的路障。
这是我的堆栈追踪:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.misc.Unsafe.allocateMemory(Native Method)
at sun.nio.fs.NativeBuffer.<init>(NativeBuffer.java:57)
at sun.nio.fs.NativeBuffers.allocNativeBuffer(NativeBuffers.java:49)
at sun.nio.fs.WindowsNativeDispatcher.asNativeBuffer(WindowsNativeDispatcher.java:1103)
at sun.nio.fs.WindowsNativeDispatcher.GetFileAttributesEx(WindowsNativeDispatcher.java:367)
at sun.nio.fs.WindowsFileAttributes.get(WindowsFileAttributes.java:309)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:51)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
at sun.nio.fs.WindowsFileSystemProvider.readAttributes(WindowsFileSystemProvider.java:193)
at java.nio.file.Files.readAttributes(Files.java:1686)
at java.nio.file.Files.isRegularFile(Files.java:2172)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:31)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)
at bsimmons_cis257_a6.DirSearch.fileVisitor(DirSearch.java:37)然后就会继续下去。
这是我的密码-
主要班:
package bsimmons_cis257_a6;
public class MainApp {
public static void main(String[] args) {
DirSearchGUI dirSearchGUI = new DirSearchGUI();
dirSearchGUI.setLocationRelativeTo( null );
dirSearchGUI.setVisible( true );
}
}GUI:
package bsimmons_cis257_a6;
public class DirSearchGUI extends javax.swing.JFrame {
public DirSearchGUI() {
initComponents();
pathOutput.setEditable(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
pathInput = new javax.swing.JTextField();
pathInputLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
pathOutput = new javax.swing.JTextArea();
buttonExit = new javax.swing.JButton();
buttonSearch = new javax.swing.JButton();
pathOutputLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Largest File Directory Search");
pathInputLabel.setText("Enter a path:");
pathOutput.setColumns(20);
pathOutput.setRows(5);
jScrollPane1.setViewportView(pathOutput);
buttonExit.setText("Exit");
buttonExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonExitActionPerformed(evt);
}
});
buttonSearch.setText("Search");
buttonSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonSearchActionPerformed(evt);
}
});
pathOutputLabel.setText("Search Results:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(buttonExit)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pathInputLabel)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(pathInput)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonSearch))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(pathOutputLabel)))
.addContainerGap(22, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(pathInputLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pathInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addComponent(pathOutputLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonExit)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void buttonSearchActionPerformed(java.awt.event.ActionEvent evt) {
DirSearch dirSearch = new DirSearch();
pathOutput.setText("");
String output = dirSearch.fileSearch(pathInput.getText());
pathOutput.append(output);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DirSearchGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DirSearchGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DirSearchGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DirSearchGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DirSearchGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton buttonExit;
private javax.swing.JButton buttonSearch;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField pathInput;
private javax.swing.JLabel pathInputLabel;
private javax.swing.JTextArea pathOutput;
private javax.swing.JLabel pathOutputLabel;
// End of variables declaration
}我的第三堂课用的方法是:
package bsimmons_cis257_a6;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DirSearch {
//attributes
private Path path;
private Path largestFile;
//constructors
//methods
public String fileSearch(String pathString){
path = Paths.get( pathString );
if (Files.exists( path ) && Files.isDirectory( path )){
fileVisitor();
}
return largestFile.toString();
}
public void fileVisitor(){
try {
DirectoryStream<Path> pathStream =
Files.newDirectoryStream(path);
for (Path p : pathStream ) {
long fileSize = 0;
if (Files.isRegularFile(p)){
if ( Files.size(p) > fileSize ){
fileSize = Files.size(p);
this.largestFile = p.getFileName();
}
} else {
fileVisitor();
}
}
} catch(IOException ex){}
}
}发布于 2016-05-06 06:46:01
堆栈跟踪指向fileVisitor()方法中的循环,我无法真正看到递归的终止条件。打开路径(在调用之间不发生变化),输入循环,当第一个if条件失败时,使用相同的路径重新输入方法,历史记录会重复,最终在StackOverflowError中结束。尝试更改fileVisitor方法以接受path作为参数(然后不需要Path path类字段),并在else分支中删除您已经访问过的部分路径。在方法的开头添加终止条件(例如,检查路径是否为空,如果为空,则为完成)--结束递归。
https://stackoverflow.com/questions/37064577
复制相似问题