小编Gre*_*989的帖子

使用Java NIO将文件从一个目录移动到另一个目录

我正在使用NIO库但是当我尝试将文件从一个目录移动到另一个目录时,我收到一个奇怪的错误.

String yearNow = new SimpleDateFormat("yyyy").format(
    Calendar.getInstance().getTime());

try {
     DirectoryStream<Path> curYearStream = 
       Files.newDirectoryStream(sourceDir, "{" + yearNow + "*}"); 
       //Glob for current year

     Path newDir = Paths.get(sourceDir + "//" + yearNow);

     if (!Files.exists(newDir) || !Files.isDirectory(newDir)) {
         Files.createDirectory(newDir); 
         //create 2014 directory if it doesn't exist
     }
}
Run Code Online (Sandbox Code Playgroud)

迭代以"2014"开头的元素并将它们移动到新目录中(newDir,也称为2014)

for (Path p : curYearStream) {
    System.out.println(p); //it prints out exactly the files that I need to move
    Files.move(p, newDir); //java.nio.file.FileAlreadyExistsException
}
Run Code Online (Sandbox Code Playgroud)

我得到了java.nio.file.FileAlreadyExistsException,因为我的文件夹(2014)已经存在.我真正想做的是将所有以"2014"开头的文件移到2014年目录中.

java directory io nio

6
推荐指数
2
解决办法
2万
查看次数

如何在JavaFX中让球从墙上弹开?

我是Javafx的新手,我正在创建一个简单的程序.我想要达到的目标是让球从墙上反弹,但我还没想出怎么做.另外,请随意留下有关我的代码的其他建议.

这是源代码:

public class GamePractice extends Application {

    public static Circle circle;
    public static Pane canvas;
    private long counter = 0;

    @Override
    public void start(Stage primaryStage) {

        canvas = new Pane();
        Scene scene = new Scene(canvas, 800, 600);

        primaryStage.setTitle("Game");
        primaryStage.setScene(scene);
        primaryStage.show();

        circle = new Circle(15,Color.BLUE);
        circle.relocate(100, 100);         

        canvas.getChildren().addAll(circle);


        Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                if (counter++ % 10 == 0)
                {
                    circle.setLayoutX(circle.getLayoutX() + 10);
                    circle.setLayoutY(circle.getLayoutY() + 10);


                    //This is what I …
Run Code Online (Sandbox Code Playgroud)

java timeline javafx javafx-2

4
推荐指数
1
解决办法
1万
查看次数

如何在JavaFX中的矩形或圆形内添加图像?

假设我们有一个名为r的矩形

Rectangle r = new Rectangle(40, 20);
Run Code Online (Sandbox Code Playgroud)

和一个称为图像的图像

Image image = new Image("...src for image");
Run Code Online (Sandbox Code Playgroud)

如何在矩形内拟合图像?另外,如果矩形移动,如何移动图像?如何为圆圈做同样的事情?代码示例非常感谢.

PS Jewelsea,我在等你,哈哈!

java geometry javafx image

4
推荐指数
1
解决办法
2万
查看次数

Java - 如何从用户输入创建类实例

我希望这个程序要求用户输入,并创建一个名称等于用户输入的类实例。然后,createMember 类将创建一个文本文件,该文件将存储用户的所有数据。我该怎么做?

下面是主要方法:

     public static void main(String[] args) {

         String input = keyboard.nextLine();        
         input = new createMember(); // Error. can't do that for some reason?
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 createMember 类

public class createMember {

public void setMembership() {
    Scanner keyboard = new Scanner(System.in);
    out.println("Username: ");
    String input = keyboard.nextLine();

    try { 
        //Creates a text file with the same name as the username where data is stored.
        Formatter x = new Formatter(input);       
    } catch (Exception e) {
        out.println("Could not create username"); …
Run Code Online (Sandbox Code Playgroud)

java class instance java.util.scanner

2
推荐指数
1
解决办法
2万
查看次数

如何在向下转换后访问基类方法?

这是一个Java Generics程序,它使用set(T t),get()和print(T t)方法实现自定义接口.

我使用的类称为Animal.它需要一个T参数(在本例中为String)并为动物命名.

我的问题是我无法访问我的print函数中的任何Animal类方法.

Animal<String> a1 = new Animal();
a1.set("Mickey");

public void print(Object o) {
  Animal oAnimal = (Animal) o; //Downcasting from Object to Animal.

  System.out.println(o.get()); //not accessible, and I don't know how to make it accessible.
//I can only access the methods from Object, not my custom class called Animal.
}
Run Code Online (Sandbox Code Playgroud)

java oop

2
推荐指数
2
解决办法
218
查看次数

在JavaFX中获取Shape对象的宽度和高度

我有需要的任何子类的方法Shape(可以是Circle,Rectangle,Polygon,Line,等...),并返回一个Shape对象.

public Shape returnShapeObject() {
   return circle1;
}
Run Code Online (Sandbox Code Playgroud)

问题是,一旦我得到了我的圆的形状对象表示,它就不再有.getRadius()方法了.它没有.getWidth().getHeight()任何一个.

如何在2d JavaFX中获取Shape对象的半径/宽度/高度呢?

java geometry 2d javafx

2
推荐指数
1
解决办法
2345
查看次数

什么条件下(真实)测试?什么时候是真是假?

我不明白为什么有人会用

while(true) {
   //do something
}
Run Code Online (Sandbox Code Playgroud)

代替

boolean condition = true;
while(condition == true) {
   //do something
}
Run Code Online (Sandbox Code Playgroud)

后者非常容易理解,而前者则不容易理解.

那么,什么条件(真实)检查?什么时候是(真)真,什么时候是假的?

java loops while-loop

1
推荐指数
1
解决办法
3万
查看次数

如何在 javascript bootstrap 中将按钮放在另一个下面

我正在使用引导程序来构建我的界面。我目前有 3 个按钮,但它们排成一行。我想要发生的事情是让它们相互依存。我如何做到这一点?

这是按钮的代码:

    this.buttonOne = document.createElement("button");
    this.buttonOne .className = "btn btn-primary";
    this.buttonOne .innerHTML = "Create One";
    this.buttonTwo = document.createElement("button");
    this.buttonTwo .className = "btn btn-default";
    this.buttonTwo .innerHTML = "Create Two";
    this.buttonThree = document.createElement("button");
    this.buttonThree .className = "btn btn-default";
    this.buttonThree .innerHTML = "Create Three";
Run Code Online (Sandbox Code Playgroud)

javascript css jquery twitter-bootstrap

1
推荐指数
1
解决办法
3036
查看次数

Object.getClass().getSuperclass()没有返回null?

我有一个功能

public void get(Object s) {

  while (s.getClass().getSuperclass() != null) {
      s = s.getClass().getSuperclass();
  }
}
Run Code Online (Sandbox Code Playgroud)

事情是,s.getClass().getSuperclass()永远不会返回null,即使Object没有超类.

我不明白为什么会发生这种情况,尽管我多次调试了这个项目.

java oop superclass

0
推荐指数
1
解决办法
1230
查看次数

列出泛型协方差

假设我们有2个不同的列表.

List 1可以采用任何Number参数(包括int,double,float和其他所有的Nu​​mber的子类).

 List<Number> l1 = new ArrayList();
Run Code Online (Sandbox Code Playgroud)

这样做非常好.

    l1.add(123);
    l1.add(123.3456);
Run Code Online (Sandbox Code Playgroud)

清单2扩展了数字.问题是我不能添加任何东西,我不明白为什么.另外,为什么我需要一个<?扩展Something>而不是仅仅创建层次结构中可以存储任何子类(如列表1)的最高类的列表?

List<? extends Number> l2 = new ArrayList();
    l2.add(123); //Error, although Integer is a subclass of Number
    l2.add(new Integer(123)); // Wrappers don't work either
    l2.add(123.456); //Error again
Run Code Online (Sandbox Code Playgroud)

java generics covariance

0
推荐指数
1
解决办法
76
查看次数

添加元素时Java泛型类错误

这是一个Generator类,它应该填充元素的ArrayList.

当我调用list.add(i)时,我不明白为什么会收到错误消息;

我也尝试将int解析为<T>并调用list.add((T)i),但这也不起作用.

public class Generator<T> {

public ArrayList<T> GenerateList(Number n) {
    ArrayList<T> list = new ArrayList<>();

    for (int i = 0; i < (int) n; i++) {
        list.add(i); //Error on this line (No suitable method found for add(int))
    }

    return list;
}
Run Code Online (Sandbox Code Playgroud)

}

java oop generics

-1
推荐指数
1
解决办法
64
查看次数

为什么我的程序在使用方法时会冻结?(JAVA)

当我在主体中使用布尔方法时,我的程序冻结并停止工作.我已经尝试将该方法放在不同的地方,但完全相同的事情发生了 - 它冻结了.

该方法非常简单,编写得很好,我不确定是什么导致了这个问题.

PS该方法位于代码的底部.

谢谢你的帮助!

编辑:现在我看着它,这是一个愚蠢的问题.再次感谢大家!

public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int stringNumber = 0;
        String[] stringArray = new String[10];


        for (int i = 0; i <= stringArray.length; i++) {

            boolean itemExists = false;
            boolean AddItem = AddItem();


            if (AddItem == true) {

                out.println("\nEnter a string");
                String input = keyboard.next();

                if (i > 0) {
                    for (int j = 0; j < stringArray.length; j++) {
                        if (input.equalsIgnoreCase(stringArray[j])) {
                            itemExists = true;
                            out.println("Item \"" …
Run Code Online (Sandbox Code Playgroud)

java methods if-statement function

-3
推荐指数
1
解决办法
250
查看次数