问题描述
我正在为HashMap编写代码,该代码在命令行中读取文件,但是我不确定是什么导致了此错误。是我不存在此文件,还是我使用错误的代码搜索文件?
float[] vertices = {
-0.5f,-0.5f,-0.0f,0.5f,0.0f,0.0f
};
FloatBuffer b = BufferUtils.createFloatBuffer(9);
b.put(vertices);
int VBO = glGenBuffers();
int VAO = glGenVertexArrays();
log.info("VBO:" + VBO + "VAO: " + VAO);
// bind the Vertex Array Object first,then bind and set vertex buffer(s),and then configure vertex attributes(s).
glBindVertexArray(VAO);
glVertexAttribPointer(0,3,GL_FLOAT,false,12,0);
glEnabLevertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,vertices,GL_STATIC_DRAW);
glBindVertexArray(0);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window))
{
// input
// -----
// render
// ------
glClearColor(0.2f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
gluseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time,but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES,3);
glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released,mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
这是它引发的异常
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class WordFrequencies {
@SuppressWarnings({"unchecked"})
public static void main(String[] args) throws Exception {
String path = new File(".").getAbsolutePath();
path=path.substring(0,path.length() - 1);
path+="alice.txt";
File file = new File(path);
String filename = "alice.txt";
Map<String,Integer> frequencies = new HashMap<String,Integer>();
Scanner sc = new Scanner(new File(filename));
while (sc.hasNext()) {
String word = sc.next();
if (frequencies.containsKey(word)) {
// increment the frequency count for this word by 1
frequencies.put(word,frequencies.get(word)+1);
} else {
// start the frequency count at 1
frequencies.put(word,1);
}
}
System.out.println(frequencies);
Set<Map.Entry<String,Integer>> entries = frequencies.entrySet();
Map.Entry<String,Integer> [] entryArray = entries.toArray(new Map.Entry[0]);
Arrays.sort(entryArray,new Comparator<Map.Entry<String,Integer>>() {
public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2) {
return o1.getKey().compareto(o2.getKey());
}
});
for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
Arrays.sort(entryArray,Integer> o2) {
return o1.getValue().compareto(o2.getValue());
}
});
for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
}
}
感谢您的帮助
解决方法
没有此类文件,并且引发了异常。 如果没有此类文件,则可以使用file.createNewFile()处理此类情况。 另外,在使用Java I / O时-始终使用try / catch处理可能的异常:
if (!file.exists()) {
try {
file.createNewFile();
// populate file,print it or do whatever you want
} catch (IOException e) {
e.printStackTrace();
}
}