新线常数

时间:2016-03-12 19:36:22

标签: r

是否存在一个与R平台无关的新线常量?我已经习惯了C#,而Environment.NewLine会在Windows上返回\r\n,而\n会返回filesonserver <- unlist(strsplit(getURL(basePath, ftp.use.epsv=F, dirlistonly=T), "\n")) 。搜索没有任何结果,但我认为必须有某些东西,以便脚本可以独立于平台。

相关问题:有没有办法检测脚本运行的平台?由于其他原因(我还没有想到),这可能是有用的。

编辑:这就是我要问的原因。我从FTP服务器下载文件,但想获取文件列表,只下载服务器上本地不存在的文件。以下是我获取文件列表的方法:

\r\n

在Windows上,文件由\n分隔。在我的Mac上(我目前正在工作),它们被\n分开。我一直在寻找一种让这个平台独立的方法。我没有试过在窗口上用package com.example.listapp; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.support.design.widget.TabLayout; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.ListFragment; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import static com.example.listapp.ItemData.Items.ORDERB; import static com.example.listapp.ItemData.Items.TABLE_NAME; import static com.example.listapp.ItemData.Items.TYPE; import static com.example.listapp.ItemData.Items.LIST_TEXT; public class MainActivity extends AppCompatActivity { private ItemData ItemsDb; String listText = null; public static String[] FROM = { LIST_TEXT }; public static int[] TO = { R.id.list_text }; public static final Uri CONTENT_URI = Uri.parse("content://" + "com.example.listapp" + "/" + TABLE_NAME); /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ItemsDb = new ListData(getApplicationContext()); Intent saveIntent = getIntent(); listText = saveIntent.getStringExtra(ItemCreate.EXTRA_MESSAGE); if (rlistText != null) { SQLiteDatabase db = ItemsDb.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(ORDERB, System.currentTimeMillis()); values.put(ITEM_TEXT, listText); db.insertOrThrow(TABLE_NAME, null, values); } Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); } public void createItem(View view) { Intent intent = new Intent(this, ItemCreate.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "List1"; case 1: return "List2"; case 2: return "List3"; } return null; } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends ListFragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Cursor cursor = getActivity().getContentResolver().query(CONTENT_URI, FROM, null, null, "_ID DESC"); ListView listItems = getListView(); ListAdapter adapter = new SimpleCursorAdapter(getContext(), R.layout.list_item, cursor, FROM, TO, 0); listItems.setAdapter(adapter); } } } 分隔,这可能有效。可能还有一种方法可以将文件列表作为矢量而不必拆分它们,这样可以完全避免这种情况......

2 个答案:

答案 0 :(得分:1)

软件包tryCatchLog具有函数determine.platform.NewLine()

因此,如果您使用此字符串而不是硬编码的“ \ n”,则新行将独立于平台运行。

答案 1 :(得分:0)

最初问题的答案似乎是没有像C#那样的新行常量。但正如评论所指出的那样,在我的案例中并不重要。直到我编辑了我可能不需要担心的细节之后,它才发生在我身上。即使包含\n返回的文件名的字符串被getURL()拆分,\r\n拆分也能在Windows上正常工作。