小编Roh*_*ish的帖子

迭代Java中的所有文件

我想让我的程序打印出我计算机上所有文件的巨大列表.我的问题是它只打印第一个硬盘驱动器的第一个文件夹中的文件,当我想要它打印我的计算机上的所有文件.我有什么想法在这里做错了吗?谢谢.

这是我使用的代码:

主要:

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        ArrayList<File> roots = new ArrayList();
        roots.addAll(Arrays.asList(File.listRoots()));


        for (File file : roots) {
            new Searcher(file.toString().replace('\\', '/')).search();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和Searcher课程:

import java.io.File;

public class Searcher {

    private String root;

    public Searcher(String root) {
        this.root = root;
    }

    public void search() {
        System.out.println(root);
        File folder = new File(root);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            String path = file.getPath().replace('\\', '/'); …
Run Code Online (Sandbox Code Playgroud)

java directory iterator file path

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

绘制矩形占绘图面板的百分之几

我想绘制矩形,只指定面板的高百分比,并且当我调整面板大小时,它应该调整自身大小.但我的问题是drawRect()只接受整数,所以当我尝试绘制矩形例如45%的面板高度时,如果面板高度小于100,它将转换为0.

我的代码很简单,所以你可以尝试一下.这是我的代码:

public class Drawer extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
            g.fillRect(10,this.getHeight()-this.getHeight()/100*45, 100, this.getHeight()-this.getHeight()/100*45);
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing awt java-2d jpanel

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

无法在Java中通过套接字发送大文件

我得到了服务器和客户端应用程序,它们在发送小文件时工作得很好,但是当我尝试发送例如700mb的电影文件时,它给了我

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)

我搜索了互联网并发现了一些关于发送大文件的教程,但是对它们不太了解,但我认为我的问题是写文件.

这是服务器用来编写我的文件的代码:

output = new FileOutputStream(directory + "/" + fileName);
            long size = clientData.readLong();
            byte[] buffer = new byte[1024];

            while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                output.write(buffer, 0, bytesRead);
                size -= bytesRead;
            }
            output.close();
Run Code Online (Sandbox Code Playgroud)

这是我的客户端用来发送文件的代码:

byte[] fileLength = new byte[(int) file.length()];  

        FileInputStream fis = new FileInputStream(file);  
        BufferedInputStream bis = new BufferedInputStream(fis);

        DataInputStream dis = new DataInputStream(bis);     
        dis.readFully(fileLength, 0, fileLength.length);  

        OutputStream os = socket.getOutputStream(); …
Run Code Online (Sandbox Code Playgroud)

java sockets

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

将方法提交到ExecutorService Java

所以我在这里有这个代码,它将我的"NamePrinter"runnable类添加为ExecutorService任务.一切都很好,但我想摆脱额外的课程.所以我想问有没有办法向ExexutorService提交方法?

这是我目前的代码:

public void start(){
        Collection<Future<?>> futures = new LinkedList<>();
        final ExecutorService es = Executors.newFixedThreadPool(poolSize);

        for (int i = 0; i < 10; i++) {
        futures.add(es.submit(new NamePrinter(name)));
        }

        es.shutdown();
}
Run Code Online (Sandbox Code Playgroud)

但我希望它是这样的:

public void start(){
            Collection<Future<?>> futures = new LinkedList<>();
            final ExecutorService es = Executors.newFixedThreadPool(poolSize);

            for (int i = 0; i < 10; i++) {
            futures.add(es.submit(namePrint(i+"name"))); // This code doesn't work, I just made it so you could understand what I mean.
            }

            es.shutdown();
    }

public void namePrint(String name){  // I'd …
Run Code Online (Sandbox Code Playgroud)

java methods executorservice

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

android中的Java Timer

怎么能在android中完成?

public final Timer timer = new Timer(10, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {  
          // Do task here       
        }
    });
Run Code Online (Sandbox Code Playgroud)

我需要能够调用timer.start(); 来自计时器所在的活动.

java android timer

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

更改图标的形状或阴影或完全删除它

有谁知道如何摆脱图标阴影?或者至少改变它的形状?我首先想到它是touchWiz的东西,但后来我检查了其他图标,它们似乎有不同形状的阴影.

这是澄清我的意思的图像:

在此输入图像描述

这是我正在使用的图标:

在此输入图像描述

icons android shadow

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

检查字符串是否为web上的web地址或ip

我需要验证在TextEdit中输入的字符串是否是Web地址,例如."www.stackoverflow.com"或IP地址,例如."64.34.119.12".我试过这两种方法没有成功.我有一个名为ip的私有类变量.

方法1:

public boolean isAdress(){

        boolean isaddr = true;
        try
        {
            ip = new NetTask().execute(""+textEdit1.getText()).get();
        }
        catch (Exception ex)
        {
            isaddr = false;
        }
        return isaddr;
    }
Run Code Online (Sandbox Code Playgroud)

方法2是我在将其发送到NetTask之前检查字符串的方法.

public boolean isAdress(){
        String adress = textEdit1.getText().toString();
        boolean isaddr = true;
        if (adress.length() > 0) {
            String[] nums = adress.split(".");
            if (nums.length == 4) {
                for (String str : nums) {
                    int i = Integer.parseInt(str);
                    if ((i < 0) || (i > 255)) {
                        isaddr = false;
                    }
                }
            } 
        }
        return …
Run Code Online (Sandbox Code Playgroud)

ip validation android ip-address

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

获取阴影/轮廓/形状的图标

我想知道是否有可能在Android的主屏幕上获得阴影/形状的图标?我知道当你向主屏幕添加一些自定义图标然后移动它时你会得到该图标的轮廓.(至少在samsungs TouchWiz 4+和API 11+中).那么有没有办法在主屏幕上获取图标的形状和位置?

icons android shape outline homescreen

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

ListView不会在notifyDataSetChanged上刷新

ListViewnotifyDataSetChanged();调用时似乎没有刷新,但在适配器再次设置时会刷新.

在我的Activity中,onCreate我初始化了我的ListView和我的适配器.然后我有这个Hanler每秒检查新值.listview.setAdapter(arrayAdapter);有效,但arrayAdapter.notifyDataSetChanged();没有做任何事情.

这是代码:

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                     lv = (ListView) findViewById(R.id.listView1);

            arrayAdapter = new ArrayAdapter<Integer>(this,android.R.layout. simple_list_item_1, myIntegers);
            lv.setAdapter(arrayAdapter);
    }

    private Handler handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                if (msg.what == DIS){
                    handler.sendEmptyMessageDelayed(DIS, 1000);
                    if(!refresh()){
                        handler.removeMessages(DIS);
                    }
                }
            }
        };

   public boolean refresh(){

        if(ports.isEmpty()){
            return false;
        }else{
            listview.setAdapter(arrayAdapter); //WORKS
    arrayAdapter.notifyDataSetChanged(); // DOESN'T WORK
            return true;
            }
    }
Run Code Online (Sandbox Code Playgroud)

所以我想知道如何使用它notifyDataSetChanged,因为我读到它是正确的方法,并且无论如何即使setAdapter …

android listview adapter

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

裁剪图像而不创建新文件

我有这段代码从图库中获取图像,然后将其传递给意图进行裁剪.一切工作正常,但这个无论是在画廊创造了新的裁剪图像或裁剪一个替换旧的形象,但我想要做的就是让新的裁剪后的图像在我的节目临时存储,直到它再次被用户更改.

这是我的代码:

Uri selectedImage = imageReturnedIntent.getData();

final Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setData(selectedImage);
                intent.putExtra("outputX", width);
                intent.putExtra("outputY", height);
                intent.putExtra("aspectX", width);
                intent.putExtra("aspectY", height);
                intent.putExtra("scale", true);
                intent.putExtra("noFaceDetection", true);
                intent.putExtra("output", selectedImage); // with this enabled it replaces the original image and without it creates new one in gallery.
                startActivityForResult(intent, 23);
Run Code Online (Sandbox Code Playgroud)

android crop bitmap android-intent

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