Java和FTP编辑在线文本文件 [英] Java and FTP to edit online text files

查看:104
本文介绍了Java和FTP编辑在线文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java swing程序中,我使用Scanner和BufferedWriter读取,编辑各种文本文件并将其保存在本地文件夹中.有什么简单的方法可以保留当前代码,但是使用FTP编辑Web文件而不是本地文件?谢谢大家.

In my Java swing program, I read, edit and save various text files in a local folder using Scanner and BufferedWriter. Is there an easy way I can keep my current code, but, using FTP, edit a web file rather than a local file? Thanks everyone.

推荐答案

我试图达到同样的目的,这些问题的答案对我有很大帮助:

I tried to achieve the same and the answers to those questions helped me a lot:

在Java (标记为右边的代码显示了如何向InputStream添加自定义字符串)

Adding characters to beginning and end of InputStream in Java (the one marked as right shows how to add a custom string to the InputStream)

从android上载文件到FTP服务器手机?(Kumar Vivek Mitra的手机显示了如何上传文件)

Uploading a file to a FTP server from android phone? (the one from Kumar Vivek Mitra shows how to upload a file)

我在在线文件末尾添加了新文本,如下所示:

I added new text to the end of my online file like this:

FTPClient con = null;

        try {
            con = new FTPClient();
            con.connect(Hostname);

            if (con.login(FTPUsername, FTPPassword)) {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);

                InputStream onlineDataIS = urlOfOnlineFile.openStream();

                String end = "\nteeeeeeeeeeeeeeeeest";
                List<InputStream> streams = Arrays.asList(
                        onlineDataIS,
                        new ByteArrayInputStream(end.getBytes()));
                InputStream resultIS = new SequenceInputStream(Collections.enumeration(streams));

                // Stores a file on the server using the given name and taking input from the given InputStream.
                boolean result = con.storeFile(PathOfTargetFile, resultIS);
                onlineDataIS.close();
                resultIS.close();
                if (result) Log.v("upload result", "succeeded");
                con.logout();
                con.disconnect();
            }
            return "Writing successful";
        } catch (IOException e) {
             // some smart error handling
        }

希望有帮助.

这篇关于Java和FTP编辑在线文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆