如何在没有完整路径的情况下传递文件?

时间:2018-06-28 07:57:37

标签: java file servletcontextlistener

我为通过Java程序进行文件更新创建了简单程序

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.isitokapp.isitok.Fragments.HomeFragment">

<RelativeLayout
    android:id="@+id/teamOkSection"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <TextView
        android:id="@+id/teamOkTopText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_alignParentTop="true"
        android:text="Team OK"
        android:textSize="11sp"
        android:layout_marginLeft="8dp"/>

    <LinearLayout
        android:id="@+id/teamOkProfilesSection"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/teamOkTopText"
        android:layout_marginTop="4dp"
        android:orientation="horizontal"
        android:layout_marginLeft="8dp">

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/teamOkAll"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_below="@+id/teamOkTopText"
        android:layout_marginTop="6dp"
        android:background="#DEDEDE">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

        <TextView
            android:id="@+id/teamOkAllText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:text="TÜMÜ"
            android:textColor="#303030"
            android:textSize="11sp"/>

    </RelativeLayout>

</RelativeLayout>

<LinearLayout
    android:id="@+id/tab_view"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#FFFFFF"
    android:layout_below="@+id/teamOkSection"
    android:orientation="horizontal">

</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/intergalactic_RV"
    android:layout_below="@id/tab_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF">
</android.support.v7.widget.RecyclerView>
<TextView
    android:id="@+id/feedNotFoundText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Paylaşım bulunamadı."
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:visibility="gone"/>

<ProgressBar
    android:id="@+id/loading_bar"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:indeterminateDuration="4000"
    android:visibility="gone"
    /></RelativeLayout>

哪个工作成功
但是然后我尝试在ServletContextListener的帮助下做同样的事情,但是在Servlet一切正常的情况下,除了我必须像这样放置完整文件路径

public class AppendToFileExample {

private static final String FILENAME = "TestFile.txt";

public static void main(String[] args) {

    BufferedWriter bw = null;
    FileWriter fw = null;

    try {

        String data = " This is new content after edit";

        File file = new File(FILENAME);
        fw = new FileWriter(file, true);
        bw = new BufferedWriter(fw);
        bw.write(data);
        System.out.println("Done");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fw != null)
                fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        }
    }
  }

有了这个完整的路径,它可以正常工作,但是当我尝试仅传递这样的文件名时,它不会更新文件

 String FILENAME = "C:\\Users\\admin\\workspacetasks\\UpdateText\\TestFile.txt";

所以我的问题是如何在没有完整文件路径的情况下传递文件。是否还有其他方法或其他东西?或任何链接供参考? 谢谢。

下面是我的ServletContextListener

 String FILENAME = "test.txt";

然后将在Java程序中调用该程序在这里的方法

public class StartupListener implements ServletContextListener {

private static Logger log = Logger.getLogger(StartupListener.class);
@Override
public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("contextInitialized");
    UpdateTextFile updateTextFile = new UpdateTextFile();   
    System.out.println("Before");
    updateTextFile.exec();
    System.out.println("after");
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    }
}

2 个答案:

答案 0 :(得分:0)

如果您的代码没有引发任何异常,则肯定是在不考虑您所想的位置创建了文件。不管是什么,都会相对于应用程序的当前工作目录创建具有相对路径的文件。对于servlet容器,它肯定不在webapp自己的目录内的任何位置。例如,在Tomcat中,它可能是Tomcat的bin目录。

NB:

  1. 与@SilvanBregy现在删除的答案相反,应用程序的CWD不一定与它自己的JAR文件所在的位置有关,并且与@soufrk的评论恰恰相反。与类路径无关。

  2. exists()测试和createNewFile()调用都完全浪费时间。 new FileOutputStream()已经做到了,因此您将迫使系统执行两次所有操作,并删除刚刚创建的文件。

  3. 您也不需要呼叫getAbsoluteFile():仅File就足够了。

  4. 但是,您确实需要关闭BufferedWriter

答案 1 :(得分:-1)

是的。。如果将来有人遇到相同的问题,
只需右键单击程序运行方式>运行配置>左面板上的Tomcat 8>参数,然后根据需要设置其他工作目录...
不知道这样做的正确方法,但是那就是我得到的..如果有人在不更改工作目录的情况下获得了其他任何方式,那么请分享其他信息(如果我得到的话),我会发布它

相关问题